A
Andrew
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
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