Many people have already given the correct answers here. I wanted to add an example that I found illuminating. Read the perldoc perlref documentation for more details .
Your problem is ambiguous, you have two operations $$ and [0] working on the same identifier _ , and the result depends on which operation is performed first. We can make it less ambiguous by using the support braces ${ ... } . $$_[0] could (for a person anyway) possibly mean:
${$$_}[0] - play the scalar $_ , then take its first element.${$_[0]} - take element 0 array @_ and @_ it.
As you can see, these two cases relate to completely different variables, @_ and $_ .
Of course, for Perl this is not ambiguous, we just get the first option, since dereferencing is done before the key search. We need supportive curly braces to override this dereferencing, and therefore your example does not βworkβ without parentheses.
You may consider somewhat less confusing functionality for your routine. Instead of trying to do two things at once (get an argument and dereference it), you can do this in two steps:
sub foo { my $n = shift; print $$n; }
Here we take the first argument off @_ with shift , and then play it out. Clean and simple.
Most often, you will not use references to scalar variables. And in these cases, you can use the arrow operator ->
my @array = (1,2,3); foo(\@array); sub foo { my $aref = shift; print $aref->[0]; }
I believe that using the arrow operator is preferable to the $$ syntax.
TLP
source share