The code is from "Modern Perl Fourth Edition", "Book version P1.0--October 2015".
It's in chapter 3 - page 81 of the paper version, under the heading "Debugging Nested Data Structures". The book is also available in a PDF version that can be downloaded at no cost.
use Data::Dumper;
my $complex_structure = {
numbers => [ 1 .. 3 ];
letters => [ 'a' .. 'c' ],
objects => {
breakfast => $continental,
lunch => $late_tea,
dinner => $banquet,
},
};
print Dumper( $my_complex_structure );
"This might produce something like the following:"
$VAR1 = {
'numbers' => [
1,
2,
3
],
'letters' => [
'a',
'b',
'c'
],
'meals' => {
'dinner' => bless({...}, 'Dinner'),
'lunch' => bless({...}, 'Lunch'),
'breakfast' => bless({...}, 'Breakfast'),
},
};
First, there are errors in the "$complex_structure" code.
There should be a comma, not a semicolon after the "numbers" line.
The variables $continental, $late_tea, and $banquet are not declared or initialized.
Dumper() is called with a wrong name : "$my_complex_structure".
The variable $objects in the code somehow becomes 'meals'in the output.
I'm not sure this is wrong, but wonder where the "bless"es come from.
Due to the errors, I can't really determine what the author meant to demonstrate, but "Objects" aren't covered until chapter 7 of the book
Here is what I wrote as an attempt to fix the (simplified) example, considering material in earlier examples about hash references and anonymous hashes:
use Data::Dumper;
my $continental = {entree => 'eggs', side => 'hash browns'};
my $complex_structure =
{
numbers => [1 .. 3],
letters => ['a' .. 'c'],
objects => {
breakfast => $continental,
},
};
print Dumper($complex_structure);
I had an error in the previous call, and, after fixing it, this new code "works" in producing output as expected, but I'm still puzzled - especially about the "bless"es seen in the book's output.
I have no experience with Data::Dumper.
Does the original example make enough sense so that it can somehow be modified to run and produce what the author expected? Should there be any "bless"es expected when something like "$complex_structure" in the book's example is dumped?
The
bless
you're seeing in the sample output is because the thingies (perldoc's wording, not mine!) referred to by the references being dumped were "blessed", i.e. objects. If you callbless
on a reference yourself and dump it, you will see the same thing.And, in fact, we do: