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; }
In reply to Hash dereferencing in a loop by willjones
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |