I get the following error message when I try to compile this code with use strict turned on:
sub failsToCompile {
my $hashRef = shift;
my $val;
foreach my $key (keys %$hashRef) {
$val .= $hashRef{$key};
}
return $val;
}
Error Message: Global symbol "%hashRef" requires explicit package name ...
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;
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|