Re: substr forces scalar context with array argument



>"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"?)

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).

>
>> 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.
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. Although, admittedly, I may be wrong about that...
particularly, if this is covered somewhere else in the docs.

andrew

.


Quantcast