$hash = { 'Man' => 'Bill',
'Woman' => 'Mary,
'Dog' => 'Ben'
};
What exactly do Perl's “anonymous hashes” do?
$hash = { 'Man' => 'Bill',
'Woman' => 'Mary,
'Dog' => 'Ben'
};
What exactly do Perl's “anonymous hashes” do?
You use an anonymous hash when you need reference to a hash and a named hash is inconvenient or unnecessary. For instance, if you wanted to pass a hash to a subroutine, you could write
but if there is no need to access the hash through its name
%hash
you could equivalently writeThis comes in handy wherever you need a reference to a hash, and particularly when you are building nested data structures. Instead of
you can write just
and to add a member,
is a lot neater than
and of course, even if a hash is created with a name, if its reference is passed elsewhere then there may be no way of using that original name, so it must be treated as anonymous. For instance in
$crew_member
is now effectively a reference to an anonymous hash, regardless of how the data was originally constructed. Even if the data is (in some scope) still accessible as%person1
there is no general way of knowing that, and the data can be accessed only by its reference.Anything "anonymous" is a data structure that used in a way where it does not get a name.
Your question has confused everyone else on this page, because your example shows you giving a name to the hash you created, thus it is no longer anonymous.
For example - if you have a subroutine and you want to return a hash, you could write this code:-
since it has no name there - it is anonymous. Read on to unwind the extra confusion other people have added on this page by introducing references, which are not the same thing.
This is another anonymous hash (an empty one):
This is an anonymous hash with something in it:
This is an anonymous (empty) array:
This is an anonymous array with something in it:
Most of the time when people use these things, they are really trying to magically put them inside of other data structures, without the bother of giving them a waste-of-time temporary name when they do this.
For example - you might want to have a hash in the middle of an array!
that array has 3 elements - the last element is a hash! ($array[2]->{foo} is 3)
Sometimes you want to don't want to pass around an entire data structure, instead, you just want to use a pointer or reference to the data structure. In perl, you can do this by adding a "\" in front of a variable;
If you're crazy, you can even have references to anonymous hashes:
and sometimes perl is clever enough to know you really meant reference, even when you didn't specifically say so, like my first "return" example:
or:-
It's quite simple. They allow you to write
instead of
(If you want to know the purpose of references, that's another story entirely.)
It is a reference to a hash that can be stored in a scalar variable. It is exactly like a regular hash, except that the curly brackets
{...}
creates a reference to a hash.Note the usage of different parentheses in these examples:
This is useful to be able to do, if you for example want to pass a hash as an argument to a subroutine:
And it is a way to create a multilevel hash: