in reply to Anonymous Data Structures: How Do They Work?

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?