Each array or hash is a collection of scalars, some or all of which can be references to other structures.
-
Lists do
not
nest like this:
@foo = (1, 3, ("hello", 5), 5.66);
For nesting, make the third element a reference to an anonymous array:
@foo = (1, 3, ["hello", 5], 5.66);
-
An example of a nested data structure (a hash of array of hashes):
$person = { # Anon. hash
"name" => "John", # '=>' is an alias for a comma
"age" => 23,
"children" => [ # Anonymous array of children
{
"name" => "Mike", "age" => 3,
},
{
"name" => "Patty","age" => 4
}
]
};
print $person->{age} ; # Print John's age
print $person->{children}->[0]->{age}; # Print Mike's age
print $person->{children}[0]{age} ; # Print Mike's age, omitting
# arrows between subscripts
-
To pretty-print
$person
above:
use Data::Dumper;
Data::Dumper->Dumper($person);
# Or,
require dumpVar.pl;
main::dumpValue($person);
Copyright © 2001 O'Reilly & Associates. All rights reserved. |
|