An answer with a few digressions.

When you assign an anonymous data structure to a scalar, you still have a scalar. This scalar is what is referred to as the reference and the data that it points to is referred to as the referent. When a scalar contains a reference, it holds a couple of pieces of data. One is the address of the referent, the other piece of data is the type of data structure. You see these two pieces when you stringify the reference:

$ perl -e 'print [qw/1 2/]' ARRAY(0x97116c)

In Perl, of course, you can't access that memory location directly (well, you can use Devel::Pointer, but I wouldn't), but it's a convenient method of managing complex data structures:

my %foo = ( name => [ qw/ Ovid is a fool / ], date => { once => 'in a', blue => 'moon' } );

In the above example, 'date' contains a reference to an anonymous hash. One interesting thing to note about anonymous structures is that the typing prohibits you from directly using them as another data structure. You have to play around with it a bit. For example, you can't directly coerce a hash reference into an array, though you can assign a hash to an array.

$ perl -e '$h={a=>2,b=>3,c=>4};print %$h' a2b3c4 $ perl -e '$h={a=>2,b=>3,c=>4};print @$h' Not an ARRAY reference at -e line 1. $ perl -e '%h=(a=>2,b=>3,c=>4);@a=%h;print@a' a2b3c4

Why does this really have anything to do with anonymous data structures? Because everything in Perl boils down to scalars and how you manage them. You can create a scalar that is a reference to an array, or stuff that reference into an another array, or even itself! All of these references will contain the same value.

$ perl -e '@a=(1,2); $foo=\@a; push@a,\@a;print $foo, $/, $a[-1]' ARRAY(0x980028) ARRAY(0x980028)

All handwaving aside, it's all scalars and it doesn't matter how you store them.

I realize that the above was a bit of a ramble, but that's my story and I'm sticking to it!

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.


In reply to (Ovid) Re: Anonymous Data Structures: How Do They Work? by Ovid
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.