what is the difference between -> and $ in Perl
where -> is infix dereference operator.
$ is also dereference operator.
what exactly meaning of this operator?
I cannot recommend strongly enough the core documentation references tutorial, perldoc perlreftut, written by the well-respected perler Mark Jason Dominus. It is concise and teaches a few simple rules for creating and using references.
I'm not saying RTFM, I'm saying, there is a great doc on this point, seek it out, it will help you!
Yes, both $ and -> are dereference operators, even though they are quite different.
Perl allows you to use references to other data. These are roughly similar to pointers in some languages. To get the original data structure, we have to dereference them. This generally involves curly braces (which can be omitted in trivial cases) and the sigil of the type we are dereferencing to. (Most values can only be dereferenced to one type, else an error is thrown).
${ $scalar_ref };
#{ $array_ref };
%{ $hash_ref };
*{ $glob_ref };
&{ $code_ref }(#args);
If the $type_ref is just a variable, the curlies can be omitted, but they are practical when we have more complex expressions.
Now the problem is to access fields in hashes or arrays, without assigning to an intermediate hash. This is where the -> operator is used:
# instead of
my %hash = %{ $hashref };
my $field = $hash{field};
# we can do this and avoid unneccessary copying :)
my $field = ${$hashref}{field}; # curlies around $hashref optional
my $field = $hashref->{field};
Similar for arrays, coderefs, and method calls on objects:
$array_ref->[$index]; $$array_ref[$index];
$code_ref->(#args); &$coderef(#args);
$object->method(#args);
Actually, what the -> operator does in the context of method calls (or what looks like them) is a bit more complex, but that doesn't seem the issue of your question.
For arrayrefs and hashrefs, you can just imagine that the $$ref[$i]-like dereference simply replaces $ref with the name of the array, which it does in a symbolic sense: $array[$i]. So the first $ sigil is that of the array element, and the second $ that of the scalar holding the reference.
My perl is a little rusty but $ always means the following is the name of a scalar. The arrow operator (->) is a very different beast. It means dereference the name and then apply the following operators. The following is an example from perlref(1):
Because of being able to omit the curlies for the simple case of $$x, people often
make the mistake of viewing the dereferencing symbols as proper operators, and wonder
about their precedence. If they were, though, you could use parentheses instead of
braces. That's not the case. Consider the difference below; case 0 is a short-hand
version of case 1, not case 2:
$$hashref{"KEY"} = "VALUE"; # CASE 0
${$hashref}{"KEY"} = "VALUE"; # CASE 1
${$hashref{"KEY"}} = "VALUE"; # CASE 2
${$hashref->{"KEY"}} = "VALUE"; # CASE 3
Case 2 is also deceptive in that you're accessing a variable called %hashref,
not dereferencing through $hashref to the hash it's presumably referencing.
That would be case 3.
They can be different ways of doing the same thing.
See http://perlmonks.org/?node=References+quick+reference; rule 3 there shows how some $ expressions can be changed to use -> instead.
Related
I saw a bit of code that went like this~
my #array = #{ $array_ref };
And I found that it works just the same if I change it to~
my #array = #$array_ref;
Likewise, this~
my %hash = (frogs => sub {print "Frogs\n"});
&{$hash{frogs}}();
Works just the same as~
%hash = (frogs => sub {print "Frogs\n"});
$hash{frogs}();
(Although I initially tried that as &hash{frogs}(); expecting it to work, with a resulting error I did not understand-- shouldn't the sigil correspond to the thing being accessed in the hash, in this case & for a subroutine?)
So I am wondering why the authors of these snippets would have written them the other way, when it's extra characters without any advantage that I can perceive.
Is something going on here which I'm not noticing? When might there be a situation where you need to use the curly braces instead of just the sigil?
You are conflating two concepts.
The first thing you need to learn is that there is there are two syntaxes for deferencing.
Circumfix Postfix
"Block syntax" "Arrow syntax"
$BLOCK EXPR->$*
#BLOCK EXPR->#*
$BLOCK[$i] EXPR->[$i]
&BLOCK() EXPR->()
... ...
Furthermore, both syntax have a simplification available to them.
Your array example is an example of a simplification available to the block syntax. If the block contains nothing but a simple scalar ($ref), you can omit the curlies.
${ $ref } # Can be simplified to $$ref
#{ $ref } # Can be simplified to #$ref
${ $ref }[$i] # Can be simplified to $$ref[$i]
&{ $ref }() # Can be simplified to &$ref()
If the block contains anything else, you can't avail yourself of this simplification.
#{ $refs{$key} } # Can't be simplified
#{ f() } # Can't be simplified
#{ my $ref = f(); $ref } # Can't be simplified
&{ $refs{$key} }() # Can't be simplified
&{ f() }() # Can't be simplified
&{ my $ref = f(); $ref } # Can't be simplified
Your code ref example, on the other hand, is an example of the switching from the block syntax to the arrow syntax. It's not a simplification per se.
"Block syntax" "Arrow syntax"
&{ $hash{frogs} }() ⇒ $hash{frogs}->()
Your code ref example is also an example of a simplification available to the arrow syntax. When the arrow is between [...] or {...}, and [...], {...} or (...), the arrow can be omitted.
$ref->{$k}->[$i] # Can be simplified to $ref->{$k}[$i]
$ref->{$k}->{$l} # Can be simplified to $ref->{$k}{$l}
$ref->{$k}->() # Can be simplified to $ref->{$k}()
$ref->[$i]->[$j] # Can be simplified to $ref->[$i][$j]
$ref->[$i]->{$k} # Can be simplified to $ref->[$i]{$k}
$ref->[$i]->() # Can be simplified to $ref->[$i]()
Finally, which should you use?
The choice of whether to the block syntax or the arrow syntax is one of personal preference, but the following conventions are usually followed to maximize readability:
The block syntax is preferred for scalar, array and hash dereferences.
$$ref is generally preferred over $ref->$*
#$ref is generally preferred over $ref->#*
%$ref is generally preferred over $ref->%*
When using the block syntax, curlies are omitted if possible.
$$ref is generally preferred over ${ $ref }
#$ref is generally preferred over #{ $ref }
%$ref is generally preferred over %{ $ref }
The arrow syntax is preferred for array element, hash element and code dereferences.
$ref->[...] is generally preferred over $$ref[...]
$ref->{...} is generally preferred over $$ref{...}
$ref->(...) is generally preferred over &$ref(...)
The arrow itself is omitted when possible, except when used to deference code.
$ref->[...][...] is generally preferred over $ref->[...]->[...]
$ref->[...]{...} is generally preferred over $ref->[...]->{...}
$ref->[...]->(...) is generally preferred over $ref->[...](...)
$ref->{...}[...] is generally preferred over $ref->{...}->[...]
$ref->{...}{...} is generally preferred over $ref->{...}->{...}
$ref->{...}->(...) is generally preferred over $ref->{...}(...)
The block syntax is preferred for array slices and hash slices. It's far less readable than the arrow syntax, but the arrow syntax for these requires Perl 5.24.
#$ref[...] is generally preferred over $ref->#[...]
#$ref{...} is generally preferred over $ref->#{...}
Your code is primarily for other people and the compiler/interpreter to read. Junior programmers often tend to treat their creations as being purely for their own consumption, as a sort of living notepad, but there really are much better ways of writing design notes than encoding them in a programming language.
It's about explaining clearly to the (human) reader what your code is doing. Your example of #$array_ref (apart from being pointless unless you really need two copies of the same list of data) is fine, but how would you read $$array_ref{key}?
Most people don't hold the table of operator precedence in their heads, so it's a lot easier to explain your intention by writing either $array_ref->{key} or ${ $array_ref{key} } depending on what you mean
Given that you mention this
&{$hash{frogs}}()
I think you're reading something that is either rather old, or written by someone who doesn't know Perl especially well. The & prefix is required only when you're referring to the subroutine as a first-class data object, such as fetching a reference, and just calling the subroutine isn't one of those
Perl allows you to omit the indirect operator -> between consecutive pairs of closing and opening brackets, but it's often clearer to include it. I generally omit it between curly and square brackets (indexing hashes or arrays) but keep it in for procedure calls
I would write that line as
$hash{frogs}->()
I ran into this and couldn't find the answer. I am trying to see if it is possible to "change" the reference of a hash. In other words, I have a hash, and a function that returns a hashref, and I want to make my hash point to the location in memory specified by this ref, instead of copying the contents of the hash it points to. The code looks something like this:
%hash = $h->hashref;
My obvious guess was that it should look like this:
\%hash = $h->hashref;
but that gives the error:
Can't modify reference constructor in scalar assignment
I tried a few other things, but nothing worked. Is what I am attempting actually possible?
An experimental feature which would seemingly allow you to do exactly what you're describing has been added to Perl 5.21.5, which is a development release (see "Aliasing via reference").
It sounds like you want:
use Data::Alias;
alias %hash = $h->hashref;
Or if %hash is a package variable, you can instead just do:
*hash = $h->hashref;
But either way, this should almost always be avoided; simply use the hash reference.
This question is really old, but Perl now allows this sort of thing as an experimental feature:
use v5.22;
use experimental qw(refaliasing);
my $first = {
foo => 'bar',
baz => 'quux',
};
\my %hash = $first;
Create named variable aliases with ref aliasing
Mix assignment and reference aliasing with declared_refs
Yes, but…
References in Perl are scalars. You are trying to alias the return value. This actually is possible, but you should not do this, since it involves messing with the symbol table. Furthermore, this only works for globals (declared with our): If you assign a hashref to the glob *hash it will assign to the symbol table entry %hash:
#!/usr/bin/env perl
use warnings;
use strict;
sub a_hashref{{a => "one", b => "two"}}
our %hash;
*hash = a_hashref;
printf "%3s -> %s\n", $_, $hash{$_} foreach keys %hash;
This is bad style! It isn't in PBP (directly, but consider section 5.1: “non-lexicals should be avoided”) and won't be reported by perlcritic, but you shouldn't pollute the package namespace for a little syntactic fanciness. Furthermore it doesn't work with lexical variables (which is what you might want to use most of the time, because they are lexically scoped, not package wide).
Another problem is, that if the $h->hashref method changes its return type, you'll suddenly assign to another table entry! (So if $h->hashref changes its return type to an arrayref, you assign to #hash, good luck detecting that). You could circumvent that by checking if $h->hashref really returns a hashref with 'HASH' eq ref $h->hashref`, but that would defeat the purpose.
What is the problem with just keeping the reference? If you get a reference, just store it in a scalar:
$hash = $h->hashref
To read more about the global symbol table, take a look at perlmod and consider perlref for the *FOO{THING} syntax, which sadly isn't for lvalues.
To achieve what you want, you could check out the several aliasing modules on cpan. Data::Alias or Lexical::Alias seem to fit your purpose. Also if you are interested in tie semantics and/or don't want to use XS modules, Tie::Alias might be worth a shoot.
as a newbie I am trying to explore perl data structures using this material from atlanta perl mongers, avaliable here Perl Data Structures
Here is the sample code that I've writen, 01.pl is the same as 02.pl but 01.pl contains additional two pragmas: use strict; use warnings;.
#!/usr/bin/perl
my %name = (name=>"Linus", forename=>"Torvalds");
my #system = qw(Linux FreeBSD Solaris NetBSD);
sub passStructure{
my ($arg1,$arg2)=#_;
if (ref($arg1) eq "HASH"){
&printHash($arg1);
}
elsif (ref($arg1) eq "ARRAY"){
&printArray($arg1);
}
if (ref($arg2) eq "HASH"){
&printHash($arg2);
}
elsif (ref($arg2) eq "ARRAY"){
&printArray($arg2);
}
}
sub printArray{
my $aref = $_[0];
print "#{$aref}\n";
print "#{$aref}->[0]\n";
print "$$aref[0]\n";
print "$aref->[0]\n";
}
sub printHash{
my $href = $_[0];
print "%{$href}\n";
print "%{$href}->{'name'}\n";
print "$$href{'name'}\n";
print "$href->{'name'}\n";
}
&passStructure(\#system,\%name);
There are several points mentioned in above document that I misunderstood:
1st
Page 44 mentions that those two syntax constructions: "$$href{'name'}" and "$$aref[0]" shouldn't never ever been used for accessing values. Why ? Seems in my code they are working fine (see bellow), moreover perl is complaining about using #{$aref}->[0] as deprecated, so which one is correct ?
2nd
Page 45 mentions that without "use strict" and using "$href{'SomeKey'}" when "$href->{'SomeKey'}" should be used, the %href is created implictly. So if I understand it well, both following scripts should print "Exists"
[pista#HP-PC temp]$ perl -ale 'my %ref=(SomeKey=>'SomeVal'); print $ref{'SomeKey'}; print "Exists\n" if exists $ref{'SomeKey'};'
SomeVal
Exists
[pista#HP-PC temp]$ perl -ale ' print $ref{'SomeKey'}; print "Exists\n" if exists $ref{'SomeKey'};'
but second wont, why ?
Output of two beginning mentioned scripts:
[pista#HP-PC temp]$ perl 01.pl
Using an array as a reference is deprecated at 01.pl line 32.
Linux FreeBSD Solaris NetBSD
Linux
Linux
Linux
%{HASH(0x1c33ec0)}
%{HASH(0x1c33ec0)}->{'name'}
Linus
Linus
[pista#HP-PC temp]$ perl 02.pl
Using an array as a reference is deprecated at 02.pl line 32.
Linux FreeBSD Solaris NetBSD
Linux
Linux
Linux
%{HASH(0x774e60)}
%{HASH(0x774e60)}->{'name'}
Linus
Linus
Many people think $$aref[0] is ugly and $aref->[0] not ugly. Others disagree; there is nothing wrong with the former form.
#{$aref}->[0], on the other hand, is a mistake that happens to work but is deprecated and may not continue to.
You may want to read http://perlmonks.org/?node=References+quick+reference
A package variable %href is created simply by mentioning such a hash without use strict "vars" in effect, for instance by leaving the -> out of $href->{'SomeKey'}. That doesn't mean that particular key is created.
Update: looking at the Perl Best Practices reference (a book that inspired much more slavish adoption and less actual thought than the author intended), it is recommending the -> form specifically to avoid the possibility of leaving off a sigil, leading to the problem mentioned on p45.
Perl has normal datatypes, and references to data types. It is important that you are aware of the differerence between them, both in their meaning, and in their syntax.
Type |Normal Access | Reference Access | Debatable Reference Access
=======+==============+==================+===========================
Scalar | $scalar | $$scalar_ref |
Array | $array[0] | $arrayref->[0] | $$arrayref[0]
Hash | $hash{key} | $hashref->{key} | $$hashref{key}
Code | code() | $coderef->() | &$coderef()
The reason why accessing hashrefs or arrayrefs with the $$foo[0] syntax can be considered bad is that (1) the double sigil looks confusingly like a scalar ref access, and (2) this syntax hides the fact that references are used. The dereferencing arrow -> is clear in its intent. I covered the reason why using the & sigil is bad in this answer.
The #{$aref}->[0] is extremely wrong, because you are dereferencing a reference to an array (which cannot, by definition, be a reference itself), and then dereferencing the first element of that array with the arrow. See the above table for the right syntax.
Interpolating hashes into strings seldom makes sense. The stringification of a hash denotes the number of filled and available buckets, and so can tell you about the load. This isn't useful in most cases. Also, not treating the % character as special in strings allows you to use printf…
Another interesting thing about Perl data structures is to know when a new entry in a hash or array is created. In general, accessing a value does not create a slot in that hash or array, except when you are using the value as reference.
my %foo;
$foo{bar}; # access, nothing happens
say "created at access" if exists $foo{bar};
$foo{bar}[0]; # usage as arrayref
say "created at ref usage" if exists $foo{bar};
Output: created at ref usage.
Actually, the arrayref spings into place, because you can use undef values as references in certain cases. This arrayref then populates the slot in the hash.
Without use strict 'refs', the variable (but not a slot in that variable) springs into place, because global variables are just entries in a hash that represents the namespace. $foo{bar} is the same as $main::foo{bar} is the same as $main::{foo}{bar}.
The major advantage of the $arg->[0] form over the $$arg[0] form, is that it's much clearer with the first type as to what is going on... $arg is an ARRAYREF and you're accessing the 0th element of the array it refers to.
At first reading, the second form could be interpreted as ${$arg}[0] (dereferencing an ARRAYREF) or ${$arg[0]} (dereferencing whatever the first element of #arg is.
Naturally, only one interpretation is correct, but we all have those days (or nights) where we're looking at code and we can't quite remember what order operators and other syntactic devices work in. Also, the confusion would compound if there were additional levels of dereferencing.
Defensive programmers will tend to err towards efforts to make their intentions explicit and I would argue that $arg->[0] is a much more explicit representation of the intention of that code.
As to the automatic creation of hashes... it's only the hash that would be created (so that the Perl interpreter and check to see if the key exists). The key itself is not created (naturally... you wouldn't want to create a key that you're checking for... but you may need to create the bucket that would hold that key, if the bucket doesn't exist. The process is called autovivification and you can read more about it here.
I believe you should be accessing the array as: #{$aref}[0] or $aref->[0].
a print statement does not instantiate an object. What is meant by the implicit creation is you don't need to predefine the variable before assigning to it. Since print does not assign the variable is not created.
What is the difference between $this, #that, and %those in Perl?
A useful mnemonic for Perl sigils are:
$calar
#rray
%ash
Matt Trout wrote a great comment on blog.fogus.me about Perl sigils which I think is useful so have pasted below:
Actually, perl sigils don’t denote variable type – they denote conjugation – $ is ‘the’, # is
‘these’, % is ‘map of’ or so – variable type is denoted via [] or {}. You can see this with:
my $foo = 'foo';
my #foo = ('zero', 'one', 'two');
my $second_foo = $foo[1];
my #first_and_third_foos = #foo[0,2];
my %foo = (key1 => 'value1', key2 => 'value2', key3 => 'value3');
my $key2_foo = $foo{key2};
my ($key1_foo, $key3_foo) = #foo{'key1','key3'};
so looking at the sigil when skimming perl code tells you what you’re going to -get- rather
than what you’re operating on, pretty much.
This is, admittedly, really confusing until you get used to it, but once you -are- used to it
it can be an extremely useful tool for absorbing information while skimming code.
You’re still perfectly entitled to hate it, of course, but it’s an interesting concept and I
figure you might prefer to hate what’s -actually- going on rather than what you thought was
going on :)
$this is a scalar value, it holds 1 item like apple
#that is an array of values, it holds several like ("apple", "orange", "pear")
%those is a hash of values, it holds key value pairs like ("apple" => "red", "orange" => "orange", "pear" => "yellow")
See perlintro for more on Perl variable types.
Perl's inventor was a linguist, and he sought to make Perl like a "natural language".
From this post:
Disambiguation by number, case and word order
Part of the reason a language can get away with certain local ambiguities is that other ambiguities are suppressed by various mechanisms. English uses number and word order, with vestiges of a case system in the pronouns: "The man looked at the men, and they looked back at him." It's perfectly clear in that sentence who is doing what to whom. Similarly, Perl has number markers on its nouns; that is, $dog is one pooch, and #dog is (potentially) many. So $ and # are a little like "this" and "these" in English. [emphasis added]
People often try to tie sigils to variable types, but they are only loosely related. It's a topic we hit very hard in Learning Perl and Effective Perl Programming because it's much easier to understand Perl when you understand sigils.
Many people forget that variables and data are actually separate things. Variables can store data, but you don't need variables to use data.
The $ denotes a single scalar value (not necessarily a scalar variable):
$scalar_var
$array[1]
$hash{key}
The # denotes multiple values. That could be the array as a whole, a slice, or a dereference:
#array;
#array[1,2]
#hash{qw(key1 key2)}
#{ func_returning_array_ref };
The % denotes pairs (keys and values), which might be a hash variable or a dereference:
%hash
%$hash_ref
Under Perl v5.20, the % can now denote a key/value slice or either a hash or array:
%array[ #indices ]; # returns pairs of indices and elements
%hash{ #keys }; # returns pairs of key-values for those keys
You might want to look at the perlintro and perlsyn documents in order to really get started with understanding Perl (i.e., Read The Flipping Manual). :-)
That said:
$this is a scalar, which can store a number (int or float), a string, or a reference (see below);
#that is an array, which can store an ordered list of scalars (see above). You can add a scalar to an array with the push or unshift functions (see perlfunc), and you can use a parentheses-bounded comma-separated list of scalar literals or variables to create an array literal (i.e., my #array = ($a, $b, 6, "seven");)
%those is a hash, which is an associative array. Hashes have key-value pairs of entries, such that you can access the value of a hash by supplying its key. Hash literals can also be specified much like lists, except that every odd entry is a key and every even one is a value. You can also use a => character instead of a comma to separate a key and a value. (i.e., my %ordinals = ("one" => "first", "two" => "second");)
Normally, when you pass arrays or hashes to subroutine calls, the individual lists are flattened into one long list. This is sometimes desirable, sometimes not. In the latter case, you can use references to pass a reference to an entire list as a single scalar argument. The syntax and semantics of references are tricky, though, and fall beyond the scope of this answer. If you want to check it out, though, see perlref.
Since arrays and hashes can only contain scalars in Perl, why do you have to use the $ to tell the interpreter that the value is a scalar when accessing array or hash elements? In other words, assuming you have an array #myarray and a hash %myhash, why do you need to do:
$x = $myarray[1];
$y = $myhash{'foo'};
instead of just doing :
$x = myarray[1];
$y = myhash{'foo'};
Why are the above ambiguous?
Wouldn't it be illegal Perl code if it was anything but a $ in that place? For example, aren't all of the following illegal in Perl?
#var[0];
#var{'key'};
%var[0];
%var{'key'};
I've just used
my $x = myarray[1];
in a program and, to my surprise, here's what happened when I ran it:
$ perl foo.pl
Flying Butt Monkeys!
That's because the whole program looks like this:
$ cat foo.pl
#!/usr/bin/env perl
use strict;
use warnings;
sub myarray {
print "Flying Butt Monkeys!\n";
}
my $x = myarray[1];
So myarray calls a subroutine passing it a reference to an anonymous array containing a single element, 1.
That's another reason you need the sigil on an array access.
Slices aren't illegal:
#slice = #myarray[1, 2, 5];
#slice = #myhash{qw/foo bar baz/};
And I suspect that's part of the reason why you need to specify if you want to get a single value out of the hash/array or not.
The sigil give you the return type of the container. So if something starts with #, you know that it returns a list. If it starts with $, it returns a scalar.
Now if there is only an identifier after the sigil (like $foo or #foo, then it's a simple variable access. If it's followed by a [, it is an access on an array, if it's followed by a {, it's an access on a hash.
# variables
$foo
#foo
# accesses
$stuff{blubb} # accesses %stuff, returns a scalar
#stuff{#list} # accesses %stuff, returns an array
$stuff[blubb] # accesses #stuff, returns a scalar
# (and calls the blubb() function)
#stuff[blubb] # accesses #stuff, returns an array
Some human languages have very similar concepts.
However many programmers found that confusing, so Perl 6 uses an invariant sigil.
In general the Perl 5 compiler wants to know at compile time if something is in list or in scalar context, so without the leading sigil some terms would become ambiguous.
This is valid Perl: #var[0]. It is an array slice of length one. #var[0,1] would be an array slice of length two.
#var['key'] is not valid Perl because arrays can only be indexed by numbers, and
the other two (%var[0] and %var['key']) are not valid Perl because hash slices use the {} to index the hash.
#var{'key'} and #var{0} are both valid hash slices, though. Obviously it isn't normal to take slices of length one, but it is certainly valid.
See the slice section of perldata perldocfor more information about slicing in Perl.
People have already pointed out that you can have slices and contexts, but sigils are there to separate the things that are variables from everything else. You don't have to know all of the keywords or subroutine names to choose a sensible variable name. It's one of the big things I miss about Perl in other languages.
I can think of one way that
$x = myarray[1];
is ambiguous - what if you wanted a array called m?
$x = m[1];
How can you tell that apart from a regex match?
In other words, the syntax is there to help the Perl interpreter, well, interpret!
In Perl 5 (to be changed in Perl 6) a sigil indicates the context of your expression.
You want a particular scalar out of a hash so it's $hash{key}.
You want the value of a particular slot out of an array, so it's $array[0].
However, as pointed out by zigdon, slices are legal. They interpret the those expressions in a list context.
You want a lists of 1 value in a hash #hash{key} works
But also larger lists work as well, like #hash{qw<key1 key2 ... key_n>}.
You want a couple of slots out of an array #array[0,3,5..7,$n..$n+5] works
#array[0] is a list of size 1.
There is no "hash context", so neither %hash{#keys} nor %hash{key} has meaning.
So you have "#" + "array[0]" <=> < sigil = context > + < indexing expression > as the complete expression.
The sigil provides the context for the access:
$ means scalar context (a scalar
variable or a single element of a hash or an array)
# means list context (a whole array or a slice of
a hash or an array)
% is an entire hash
In Perl 5 you need the sigils ($ and #) because the default interpretation of bareword identifier is that of a subroutine call (thus eliminating the need to use & in most cases ).