substr forces scalar context with array argument



In the code below, the array '@tmp' with two elements is treated as its
scalar (the number 2), when used as argument to substr. When I
syntactically break up '@tmp' into '$tmp[0]' and '$tmp[1]', substr does
what i want. Why is this so? Is there a way to force list context
(besides explicit enumeration)? I can't seem to find any mention of
this anywhere. Could it be an oversight/bug in substr? (probably not,
given the thoroughness that has gone into the development of perl
functions, but this is unperl-like limitation :-)

my $s='abracadabra';
my ($offset,$length)=@tmp=(3,4);

my @code=(
'substr($s, $offset, $length)',
'substr($s, @tmp)',
'substr($s, @tmp[0,1])',
'substr($s, $tmp[0], $tmp[1])'
);


foreach $c (@code) {
print "\n", $c, ' ---> ', eval($c);
}


#-------------- output -------------------

substr($s, $offset, $length) ---> acad
substr($s, @tmp) ---> racadabra
substr($s, @tmp[0,1]) ---> cadabra
substr($s, $tmp[0], $tmp[1]) ---> acad

#------------ end output ----------------

( Also, FWIW, using '@tmp[0,1]' yields a weird result I can't explain )

TIA

andrew

.



Relevant Pages

  • Re: substr forces scalar context with array argument
    ... Andrew wrote: ... > scalar, when used as argument to substr. ... array, yes, it converts that array to a scalar. ...
    (comp.lang.perl.misc)
  • chinese and arrays
    ... beginner level script, with just functions. ... taking into an array the text substr took ... I think it's no longer chinese ...
    (comp.lang.php)
  • Re: Text Manipulation help needed
    ... u> my @array = qw; ... perldoc -f splice for more. ... think) substr added a 4th arg to match splice's 4th arg. ...
    (perl.beginners)
  • Re: split n characters into n chunks
    ... you can use substr $myword, 1,3 function ... my $word = "thequickbrown" ... string and push each character into array, then loop through the array, ...
    (perl.beginners)
  • process takes a long- BULK COLLECT
    ... TYPE ARRAY IS TABLE OF VARCHAR2 ... SUBSTR AS minutos, ... SUBSTR AS segundos, ... (cliente, segmento, servicio, ...
    (comp.databases.oracle.server)