substr forces scalar context with array argument
- From: "Andrew" <hawk007@xxxxxxxxx>
- Date: 29 Nov 2005 07:18:51 -0800
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
.
- Follow-Ups:
- Re: substr forces scalar context with array argument
- From: xhoster
- Re: substr forces scalar context with array argument
- From: Paul Lalli
- Re: substr forces scalar context with array argument
- From: Paul Lalli
- Re: substr forces scalar context with array argument
- Prev by Date: Re: Addition of same value
- Next by Date: Re: How to modify a XML file using Perl?
- Previous by thread: Addition of same value
- Next by thread: Re: substr forces scalar context with array argument
- Index(es):
Relevant Pages
|