Anonymous data structures

We can use anonymous data structures to create complex data structures, to avoid having to declare many temporary variables. Anonymous arrays are created by using square brackets instead of round ones. Anonymous hashes use curly brackets instead of round ones.

# the old two-step way:
my @array = qw(a b c d);
my $array_ref = \@array;

# if we get rid of $array_ref, @array will still hang round using up
# memory.  Here's how we do it without the intermediate step, by 
# creating an anonymous array:

my $array_ref = ['a', 'b', 'c', 'd'];

# look, we can still use qw() too...

my $array_ref = [qw(a b c d)];

# more useful yet, we can put these anon arrays straight into a hash:

my %transport = (
        'cars'          =>   [qw(toyota ford holden porsche)],
        'planes'        =>   [qw(boeing harrier)],
        'boats'         =>   [qw(clipper skiff dinghy)],
);

The same technique can be used to create anonymous hashes:

# The old, two-step way:

my $hash = (
	a	=>	1,
	b	=>	2,
	c	=>	3
);
my $hash_ref = \$hash;
	
# the quicker way, with an anonymous hash:
my $hash_ref = {
	a	=>	1,
	b	=>	2,
	c	=>	3
};