now that's extra weird, because introspecting with Devel::Peek reveals that the implementation is (falsely) differentiating between different kinds of 0s and NULLs.

The "other kind of 0 or NULL" is an undefined scalar.

exists differentiates between what Dump shows as SV = 0 (underlying NULL in the array) and everything else (underlying non-NULL in the array).

Say you have this variable:

my @a; $a[1] = undef; $a[2] = 123;

Devel::Peek shows

SV = PVAV(0x**6adf0) at 0x**99148 REFCNT = 1 FLAGS = () ARRAY = 0x**8b9c0 FILL = 2 MAX = 3 FLAGS = (REAL) Elt No. 0 SV = 0 Elt No. 1 SV = NULL(0x0) at 0x**69538 REFCNT = 1 FLAGS = () Elt No. 2 SV = IV(0x**696f0) at 0x**69700 REFCNT = 1 FLAGS = (IOK,pIOK) IV = 123

(Replaced the common start of the addresses with ** to help with readability.)

The underlying array buffer of @a looks like this:

ARRAY @ 0x**8b9c0 +-------------+ Head 0 | NULL | @ 0x**69538 +-------------+ +-------------+ 1 | 0x**69538 --------->| NULL | Body +-------------+ +-------------+ 2 | 0x**69700 ------+ | 1 | REFCNT +-------------+ | +-------------+ 3 | | | | ... | FLAGS +-------------+ | +-------------+ | | SVt_NULL | TYPE | +-------------+ | | | | +-------------+ | | Head Body | @ 0x**69700 @ 0x**696f0 | +-------------+ +-----------+ +-->| 0x**696f0 --------->| ... +-------------+ | 1 | REFCNT +-------------+ | SVf_IOK|... | FLAGS +-------------+ | SVt_IV | TYPE +-------------+ | ... | +-------------+

(Technically, the body of an SVt_IV scalar is part of the same memory block as its head, but illustrating that would just complicated things needlessly.)

What follows SV = is the type of scalar.

The second number in SV = TYPE(0xBBB) at 0xHHH is the address of the scalar's head (i.e. of the scalar itself), and the first number is the address of the scalar's body. Scalars of type NULL have no body. As such, the pointer to the scalar's body in such scalars will be NULL (0x0).


In reply to Re^7: Using exists to check element in array 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.