If I have a hash
my %h = (
secret => 1;
);
and I know that is only is one key in the hash, but I don\'t know what it is called.
Let's have a look at
my ($key) = %h;
Hashes and arrays are not as different as they seem. They are both closely related to lists. Using lists is the way they are initialized normally, and =>
is mostly an alias for ,
with the only difference that it treats its left operand quoted implicitly. Perl stops only if you forget the quotation of its right operand, so the following both lines will be accepted:
my %h = (a=>b=>c=>'d');
my @a = (a=>b=>c=>'d');
Well, did you ever try this?
my %h = ('key');
...or this:
my @a = ('value');
my %h = @a;
The hash above may look a bit strange, but it's just a key with the value undef
.
Because it's most likely that you will ask how to access the single value, I suggest to use:
my ($key, $value) = %h;
...or even simpler:
my ($key) = %h;
That's what we started with.