It does make sense to me to consider $a[-1] as undef, as what happened to line 2.

The problem is, as Aristotle says, passing it to a function means it must be aliased. Aliasing an array element means taking a reference to it. Referencing a non-existent array element means autovivifying it. That's where the "modification" is being attempted. For a minimal example consider:

perl -e'\$a[0]' perl -e'\$a[-1]'

Unlike taking a reference to a non-existent array element, taking the value of a non-existent array element does not result in autovivification. To see the difference:

perl -le '$a[0]; print scalar @a' perl -le '\$a[0]; print scalar @a'
A non-existent array element is just always an undefined value. I think that's intuitive and as it should be.

I wonder how (or if) it would make sense to change the behavior of negative array elements during autovivification. At first, it seems the only way to even consider doing it differently might be to special case -1 to mean 0 if the array is empty. But, that's not very useful. It would be like a one-off push(), and, you can be sure that there would be no end to people asking why $a[-1] = 42 for (1 .. 10); didn't work like they expected. That's probably reason enough not to do it. And, it illustrates the real issue... if you special case -1, what about -2? And so on? I guess it could be handled sensibly (for some probably senseless definition of "sensible") if any index less than the negative of the number of elements in @array were treated as $array[$#array + 1]. But still, I don't see much use in it. And it would lead to code like $a[-$_] = 42 for (1 .. 10); which I'm quite sure I don't ever want to see.

As for the difference between print() and my own functions, I can definitely live with that. It seems that print probably does what I mean in that case. But, to get my own function to behave the same way, Perl would have to provide a way for us to really pass-by-value (rather than just emulating it after the fact through an explicit copy.) And who needs it? In the rare cases where you want to do something like this you could take another approach instead. Any of several other approaches depending on the behavior you needed. For some examples:

foo( [$a[-1]]->[0] ); do { foo(my $t = $a[-1]) }; foo( defined $a[-1] ? $a[-1] : undef ); foo( "$a[-1]" );
All of which are ugly and might benefit from a short comment mentioning why it's being done this way. But, again, infrequent constructs like this aren't such a big deal and seem a worthwhile trade-off to the alternatives.

† PHP behaves like this when no index is given. I.e. $a[] = 42; in PHP is equivalent to push(@a, 42) in Perl. I've actually found it to be a useful shorthand and wouldn't mind it's addition to Perl. At the very least, it would give us an excuse to replay lots of rounds golf.

-sauoq
"My two cents aren't worth a dime.";

In reply to Re: Modification of non-creatable array value attempted by sauoq
in thread Modification of non-creatable array value attempted by pg

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.