views:

260

answers:

6

What's a simple reference or cheat sheet for nested data structures in Perl?

A: 

Perhaps the most definitive reference is perlref:

I agree that having a simple reference or cheat sheet would be nice.

The examples at the end of perldsc (Perl data structures cookbook) serve as a reasonable quick reference.

dreeves
+4  A: 

perldoc perldsc is helpful to understand those nested structures

PERLDSC(1)            User Contributed Perl Documentation           PERLDSC(1)

NAME
       perldsc - Perl Data Structures Cookbook

DESCRIPTION
       The single feature most sorely lacking in the Perl programming language
       prior to its 5.0 release was complex data structures.  Even without
       direct language support, some valiant programmers did manage to emulate
       them, but it was hard work and not for the faint of heart.  You could
       occasionally get away with the $m{$AoA,$b} notation borrowed from awk
       in which the keys are actually more like a single concatenated string
       "$AoA$b", but traversal and sorting were difficult.  More desperate
       programmers even hacked Perlâs internal symbol table directly, a strat-
       egy that proved hard to develop and maintain--to put it mildly.
Vinko Vrsalovic
There is nothing nasty about them.
Sinan Ünür
@Vinko, maybe you are doing it wrong then. :)
brian d foy
@brian, very likely :)
Vinko Vrsalovic
@Vinko: I prefer 'nasty' to 'nested', actually. It's honest, and peer pressure be damned. (I also think you should never delete comments that have responses. It makes the thread too hard to follow.) Damned if you do and damned if you don't apparently. @Sinan and brian: as powerful and useful as references are in Perl, their syntax is not especially easy to remember. Like anything else, you get used to it, but I don't think it's unreasonable to say it's one of the less pleasant parts of Perl. If anything it seems a bit fanboy-ish to me to deny that they are difficult.
Telemachus
A: 

perldsc a list of recipes.

Matt Beldyk
A good one, but note that it's now available within Perl's internal documentation at `perldoc perldsc`.
Telemachus
+1  A: 

Try perldoc perlcheat

REFERENCES
 \ references $$foo[1] aka $foo->[1]
 $@%&* dereference $$foo{bar} aka $foo->{bar}
 [] anon. arrayref ${$$foo[1]}[2] aka $foo->[1]->[2]
 {} anon. hashref ${$$foo[1]}[2] aka $foo->[1][2]
 \() list of refs
plastic chris
+8  A: 

An excellent beginning tutorial is perldoc perlreftut. I also highly recommend two other tutorials in the built-in Perl documentation: perldoc perllol and perldoc perldsc. (I worked through them in that order, but your mileage may vary. I also found it easier to read perldoc perlref after I worked through those tutorials, but again, you may not.)

You should always be able to get these via a terminal, if you have a normal Perl installation. But you can also get them via the web (and as downloadable pdfs) via those links.

For a cheat sheet/reference sheet, you could do a lot worse than this post on PerlMonks.

Telemachus
`perlreftut` can't be recommended enough. It should be everyone's first introduction to references, because everything gets a whole lot less mysterious when you keep perlreftut's lessons in mind. I think that I would put `perldsc` ahead of `perllol`, and maybe skip `perllol` entirely. (I generally recommend that beginners find the time to read *every* perldoc, but perllol is old and weird and confusing :)
hobbs
@Hobbs: I agree about `perlreftut`. As for `perllol`, I remember finding it easier to start with since it was *only* arrays. I don't think it's so different really than `perldsc` except that it only handles simpler structures.
Telemachus
A: 

There's http://perlmonks.org/?node=References+quick+reference.

ysth