Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Anonymous Data Structures: How Do They Work?

by jonjacobmoon (Pilgrim)
on Jan 14, 2002 at 00:29 UTC ( [id://138457]=perlquestion: print w/replies, xml ) Need Help??

jonjacobmoon has asked for the wisdom of the Perl Monks concerning the following question:

Some time ago, I dabbled in amatuer astronomy. One thing I learned while looking at an object is sometimes you can see it better by not looking directly at it. Problem with that for me was it was hard not to look directly at the object and if I did view it out of the corner of my eye, I never felt like I was really seeing it. This is how I have felt recently about anonymous data structures.

Recently, I had the opportunity to use an anonymous array appropriately in my code. I put it in, it worked, and at the time I did that I understood, or at least I thought I understood, what it was doing and how it worked. But the more I looked at the code and the more I read about anonymous arrays (in this case) the less I understood. In fact, the information I have read so far has been left me wanting more.

So, my question is how do these work, internally I mean. I understand that I create the sturcture, it has no name which leaves it without a way to call it directly. This worked in my case because I was iterating over the same array and making it anonymous allowed it to "recreate" itself anew. But HOW does Perl understand this. It is hard to imagine that Perl can access any data structure without some sort of name associated with it. Is it just sort of a "natural reference" with a reference but no name, and if that is the case how does this get handled vis-a-vis the garbage handled alogrithms.

I realize this is an esoteric subject, and I am sure my writting skills don't do it justice, but it has plagued me on and off for a week, so I was hoping the wisdom of the monks could pull me through.


I admit it, I am Paco.

Replies are listed 'Best First'.
Re: Anonymous Data Structures: How Do They Work?
by orkysoft (Friar) on Jan 14, 2002 at 01:11 UTC

    When there is nothing that references a data structure, it will go away. You can't access it, so it's not needed anymore.

    Consider this: my $array = [1,2,3];

    $array is a scalar -- it is a reference to the anonymous array created with [1,2,3]. It'll have an internal name like ARRAY(0x80cb8fc) or something, which is what is put in $array: try printing it, you'll see a string like that.

    This is called a reference, it is magic. You can't just assign $array = "ARRAY(0x80cb8fc)"; and expect it to behave like a reference. It'll be just another string this way.

    You can however, say:

    my $array = [1,2,3]; my $alias = $array; $alias->[2] = 4; print $array->[2]; # Will produce 4.

    In this example, both $array and $alias point to the same anonymous array.

    You can also say something like:

    my @array = (1,2,3); my $arrayref = \@array; print @$arrayref;
    Which will print the contents of the array.

    I recommend you read the References tutorial for more information.

    The [1,2,3] array is anonymous, since it never gets a name. The reference to it, $array, has a name, but that's not the name of the array itself.

    You'll want to use things like this for building e.g. a two-dimensional array, or an array of hashes, or a hash of arrays.

Re: Anonymous Data Structures: How Do They Work?
by Zaxo (Archbishop) on Jan 14, 2002 at 00:58 UTC

    Perl stores its data structures in reference-counted chunks of memory. When the reference count falls to zero, the memory becomes available for reuse.

    You can think of a structure's name (if any) as a reference with special semantics. The name lives in a symbol table or in the lexical scratchpad. When a name goes out of scope, the reference count is adjusted just as if an ordinary reference had been destroyed.

    I've intentionally shifted the point of view here, presenting anonymous structures as the "normal" case and names as special. I think that attitude gives a more orthogonal factoring of storage and access.

    After Compline,
    Zaxo

      Cool, we both posted at the same time, and our nodes compliment one another nicely :-) ++!
(Ovid) Re: Anonymous Data Structures: How Do They Work?
by Ovid (Cardinal) on Jan 14, 2002 at 01:38 UTC

    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.

Re: Anonymous Data Structures: How Do They Work?
by danger (Priest) on Jan 14, 2002 at 01:15 UTC
    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?

Re (tilly) 1: Anonymous Data Structures: How Do They Work?
by tilly (Archbishop) on Jan 14, 2002 at 10:44 UTC
    Here is the simple answer said without most of the technical language that the other answers gave.

    Perl's has an internal naming scheme that involves addresses in machine memory. Humans tend to go cross-eyed when they see names like that, so Perl does a lot of work to make things look like they have names that make some sense to the programmer. With anonymous data structures all that happens is that the programmer tells Perl, "I want this somewhere private" and Perl doesn't bother going through the work of giving it a name for the programmer. However Perl still keeps track of it, just like it does everything else.

    (And Perl keeps track of what you still know about, and when you drop the last reference through which you could find that data, Perl goes in and does cleanup. This is called garbage collection.)

    So it is anonymous to you, but not to Perl.

Re: Anonymous Data Structures: How Do They Work?
by MungeMeister (Scribe) on Jan 14, 2002 at 21:47 UTC
    Read chapters 1 and 2 of Advanced Perl Programming by Sriram Srinivasan. Its an O'Reilly book. There are some excellent examples and pictures to describe whats going on.

    Then, add "require 'dumpvar.pl';" at the top of your program, and use "dumpValue(ref);" to see how your data is stored. dumpValue requires a reference, so if you have "@array", you would use "\@array" as the ref.

    The reading was good, but what really helps me when I get lost in my code is to use dumpValue to see what's really going on.

    Brian
Re: Anonymous Data Structures: How Do They Work?
by torin (Sexton) on Jan 15, 2002 at 05:18 UTC

    Perhaps a non-technical analogy might be in order.

    If a house (anonymous array) is built off of a new road that doesn't have a name yet, that doesn't keep the house from existing. But if you don't have a map showing you where it is (scalar ref to anon array), then it will be rather hard to find when you want to go back to it.

    Does that help or is it clear as mud?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://138457]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (6)
As of 2024-03-28 10:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found