linked lists in perl?

Darren Duncan darren at DarrenDuncan.net
Wed Nov 6 22:20:43 CST 2002


Nathaniel said:
>Hello everyone,
>I recently acquired one of those "Essentials" pocket books; this one is called
>Data Structures I.  The chapter I am on right now deals with linked lists.  I
>admit that the extent of my search for link-list-like structs in Perl has been
>limited to scanning the index of the Camel 3rd (zilch), but does anyone know
>how linked lists could be emulated in Perl, if at all (since as far as I know
>Perl doesn't have anything like pointers...)?
>Just curious.
>Thanks,
>Nathanael

While Perl 5 doesn't have "pointers" per se, it does have "references" for variables of any data type, which are more or less the same thing.

You can either take references to existing variables and pass them around like plain scalars, or you can create new anonymous variables, where the only way to hold them is with a reference.

Perl's built-in data structures are rather unique to the language in their implementation or properties, so they are flexible enough to emulate several other things you are used to.

For example, Perl's arrays and hashes are both "sparse", part of which means they grow as needed when you stuff more values into them.  Also, with arrays you can add or remove elements to either the front or the back end easily.

I expect that some of Perl's variables make use of some linked lists in the background so that the whole array or hash don't have to be re-copied every time they grow in size.

By using Perl's references, can effectively create a multi-dimensional structure with any properties you want, which can be linked like a list or a tree, including circular references like a doubly-linked list.

I suggest that you examine what kind of behaviour you want and re-ask the list on how might be a way to get the behaviour you want.

-- Darren Duncan



More information about the Victoria-pm mailing list