Re: substr forces scalar context with array argument
- From: "Paul Lalli" <mritty@xxxxxxxxx>
- Date: 29 Nov 2005 08:08:42 -0800
Andrew wrote:
> 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.
$ perldoc -f substr
substr EXPR,OFFSET,LENGTH,REPLACEMENT
substr EXPR,OFFSET,LENGTH
substr EXPR,OFFSET
substr is expecting between 2 and 4 scalars. So when you pass it an
array, yes, it converts that array to a scalar. That's what it's
documented to do.
> 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 :-)
I don't see it as a limitation at all. How frequently are you going to
have such distinct values as an offset and a length contained in a
single array? Unless you do so very intentionally, it's just not
likely to happen.
Paul Lalli
.
- Follow-Ups:
- Re: substr forces scalar context with array argument
- From: Andrew
- Re: substr forces scalar context with array argument
- References:
- substr forces scalar context with array argument
- From: Andrew
- substr forces scalar context with array argument
- Prev by Date: Re: How to modify a XML file using Perl?
- Next by Date: Re: substr forces scalar context with array argument
- Previous by thread: substr forces scalar context with array argument
- Next by thread: Re: substr forces scalar context with array argument
- Index(es):
Relevant Pages
|