|Welcome to the Monastery|
(Ovid) Re: Anonymous Data Structures: How Do They Work?by Ovid (Cardinal)
|on Jan 14, 2002 at 01:38 UTC||Need Help??|
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:
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:
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.
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.
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!
Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.