It is hard to imagine that Perl can access any data structure without some sort of name associated with it

Let me try to help you imagine what is going on via an example that doesn't use an anonymous structure to start with:

sub make_aref { my @array = @_; return \@array; } my $aref = make_aref(42, 13, 47); print "$aref : @$aref\n"; __END__ Output: ARRAY(0x80f6888) : 42 13 47

Here we have essentially created an anonymous array. It has no variable name attached (the name "@array" has fallen out of scope). Inside the routine we create a lexical variable "@array" (and populate it). Internally, perl creates a structure (an AV in the case of arrays) and associates this lexical array variable name with it. The reference count for this AV is one because one thing points to it (the variable "@array"). The return call takes a reference to the AV that "@array" points to, and in doing so increases the reference count of the AV (now two things point to it). Upon exiting the routine, the lexical variable "@array" falls out of scope (no longer exists) and the AV's reference count is decremented back to one (the name "@array" no longer points to it). The value we get back from the routine is a reference to this AV (which no longer has a name).

If you follow that, then it is only a small step to grasping anonymous structures --- all we do is forego the make_aref() routine altogether and simply ask perl to create a new AV and give us the reference to it.

my $aref = [42, 13, 47];

As long as $aref still holds that reference, the reference count for the AV is still one (more if we store the reference in other places as well). The AV in question will exist as long as its reference count remains above 0.

Does that assist you in imagining a reference to an unnamed structure?


In reply to Re: Anonymous Data Structures: How Do They Work? by danger
in thread Anonymous Data Structures: How Do They Work? by jonjacobmoon

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.