Re: substr forces scalar context with array argument
- From: xhoster@xxxxxxxxx
- Date: 29 Nov 2005 18:39:28 GMT
"Andrew" <hawk007@xxxxxxxxx> wrote:
> >"Andrew" <hawk...@xxxxxxxxx> 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?
> >
> >Because substr has a prototype demanding scalars.
> >
> >~/perl_misc:$ perl -le 'print prototype "CORE::substr"'
> >$$;$$
>
> Is there a reason the prototype is demanding scalars? (Or is it simply
> "the king's decree"?)
In the 2nd and 3rd slots, it makes sense to demand scalars, because it may
be natural to use (say) the scalar value (i.e. count) of a grep, map, or
array to specify the offset or length. It wouldn't seem particularly
natural to use those counts as a string (the first argument), but then
again the alternative behavior isn't natural either. If the 2nd and 3rd
arguments are interpreted as scalars, the first ones is going to be, too.
Aside from which, I am aware of no built-ins taking an array treated as a
list in which the list is heterogenous. Why would substr be different?
> Again, in my observation and experience, perl particulars are designed
> with careful consideration of all the tradeoffs, and the most optimal
> (and usually intuitive) alternative is chosen (for syntax and
> functionality).
And this case is no exception.
>
> >
> >> 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
> >
> >Nowhere does it say LIST, so I wouldn't expect it to take an array
> >interpretted as a list.
>
> Assuming that with the above you are rebutting my comment that "the
> word 'scalar' is nowhere to be found...": So the documentation does not
> mention either lists or scalars, which may leave the reader guessing.
Not a reader who knows about the format used in perldoc. If it doesn't
say LIST, it is not a LIST. Can you imagine how longs the docs would be if
they had to add a tag [And here we didn't say LIST because we didn't mean
LIST] after every other word?
> So, the purpose of my response to yours is to prevent the quick
> dismissal of the notion that the documentation /may/ need to be a bit
> more explicit.
If you proposed actual verbage to implement that greater explicitness, I'd
take you notion more seriously.
> Although, admittedly, I may be wrong about that...
> particularly, if this is covered somewhere else in the docs.
I don't know if there is a part of perldoc that describes the format by
which is describes the format by which it describes the formats it
describes. If there is, I'm glad I've never needed to consult it.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
.
- References:
- substr forces scalar context with array argument
- From: Andrew
- Re: substr forces scalar context with array argument
- From: xhoster
- Re: substr forces scalar context with array argument
- From: Andrew
- substr forces scalar context with array argument
- Prev by Date: Re: Upgrade Perl5.8.7
- Next by Date: Re: ASTM checksum
- Previous by thread: Re: substr forces scalar context with array argument
- Next by thread: Re: substr forces scalar context with array argument
- Index(es):