willjones has asked for the wisdom of the Perl Monks concerning the following question:
I get the following error message when I try to compile this code with use strict turned on:
Error Message: Global symbol "%hashRef" requires explicit package name ...sub failsToCompile { my $hashRef = shift; my $val; foreach my $key (keys %$hashRef) { $val .= $hashRef{$key}; } return $val; }
I finally discovered a work around. When I rework the code a little to be like the following sub then the error message goes away. So, I know how to get around it now, but my question is... why? Why do I have to do that? Why can't I refer to the hash that the hash reference points to in the for loop without dereferencing it first and assigning it to another variable as I did in the following sub? Also, what is making perl think this is a global symbol? Thanks in advance for input/explanations.
sub worksCompilingFine { my $hashRef = shift; my %hash = %$hashRef; my $val; foreach my $key (keys %hash) { $val .= $hash{$key}; } return $val; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Hash dereferencing in a loop
by roboticus (Chancellor) on Jun 07, 2013 at 16:46 UTC | |
|
Re: Hash dereferencing in a loop
by hdb (Monsignor) on Jun 07, 2013 at 16:42 UTC | |
by locked_user sundialsvc4 (Abbot) on Jun 08, 2013 at 14:21 UTC |