As with all lvalue context operations, the rational is that it must exist so you can assign to it (e.g. $$a = 123;)

Except not quite. Cause the element is evaluated in lvalue context in both for (@a) and f(@a) as well. So why don't they replace the NULL with a scalar?

That's cause they create and return a proxy scalar containing a reference to the array and the index. Using magic, accesses to this scalar access the original array element.

$ perl -MDevel::Peek -e'sub { Dump($_[0]) }->( $a[0] )' SV = PVLV(0x558b3a14b8a0) at 0x558b3a0f4538 REFCNT = 1 FLAGS = (GMG,SMG) <- Get and set magic IV = 0 NV = 0 PV = 0 MAGIC = 0x558b3a119460 MG_VIRTUAL = &PL_vtbl_defelem MG_TYPE = PERL_MAGIC_defelem(y) TYPE = y TARGOFF = 0 <- index TARGLEN = 1 TARG = 0x558b3a1241d8 <- array FLAGS = 0 SV = PVAV(0x558b3a0f6868) at 0x558b3a1241d8 REFCNT = 2 FLAGS = () ARRAY = 0x0 FILL = -1 MAX = -1 FLAGS = (REAL)

Creating this proxy for \ would cause issues.


In reply to Re^8: Using exists to check element in array (existence-by-reference for hashes too) by ikegami
in thread Using exists to check element in array by david

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.