Now you can use it to construct the larger list:
my $ref_to_skipper_provisions =
[ qw(blue_shirt hat jacket preserver sunscreen) ];
my @skipper_with_name = ("The Skipper", $ref_to_skipper_provisions);
Of course, you didn't actually need that scalar
temporary, either. You can put a scalar reference to an array as part
of a larger list:
my @skipper_with_name = (
"The Skipper",
[ qw(blue_shirt hat jacket preserver sunscreen) ]
);
Now let's walk through this. You've
declared @skipper_with_name, the first element of
which is the Skipper's name string, and the second
element is an array reference, obtained by placing the five
provisions into an array and taking a reference to it. So
@skipper_with_name is only two elements long, just
as before.
So, in summary, the syntax:
my $fruits;
{
my @secret_variable = ('pineapple', 'papaya', 'mango');
$fruits = \@secret_variable;
}
can be simply replaced with:
my $fruits = ['pineapple', 'papaya', 'mango'];
sub get_provisions_list {
return (
["The Skipper",
[qw(blue_shirt hat jacket preserver sunscreen)]
],
["The Professor",
[qw(sunscreen water_bottle slide_rule batteries radio)]
],
["Gilligan",
[qw(red_shirt hat lucky_socks water_bottle)]
],
);
}
my @all_with_names = get_provisions_list( );
Walking through this from the outside in, you have a return value of
three elements. Each element is an array reference, pointing to an
anonymous two-element array. The first element of each array is a
name string, while the second element is a reference to an anonymous
array of varying lengths naming the provisions—all without
having to come up with temporary names for any of the intermediate
layers.
To the caller of this subroutine, the return value is identical to
the previous version. However, from a maintenance point of view, the
reduced clutter of not having all the intermediate names saves screen
and brain space.
You can show a reference to an empty anonymous hash using an empty
anonymous array constructor. For example, if you add one
"Mrs. Howell" to that fictional
travel list, as someone who has packed rather light,
you'd simply insert:
["Mrs. Howell",
[ ]
],