Reputation: 45
Now my file1 contains the hash of hashes as shown below:
package file1;
our %hash = (
'articles' => {
'vim' => '20 awesome articles posted',
'awk' => '9 awesome articles posted',
'sed' => '10 awesome articles posted'
},
'ebooks' => {
'linux 101' => 'Practical Examples to Build a Strong Foundation in Linux',
'nagios core' => 'Monitor Everything, Be Proactive, and Sleep Well'
}
);
And my file2.pl contains
#!/usr/bin/perl
use strict;
use warnings;
require 'file1';
my $key;
my $key1;
for $key (keys %file1::hash){
print "$key\n";
for $key1 (keys %{$file1::hash{$key1}}){
print "$key1\n";
}
}
Now my question is, I get an error
"Use of uninitialized value in hash element at file2.pl"
when I try to access the hash like this:
for $key1 (keys %{$file1::hash{$key1}})
Kindly help.
Upvotes: 0
Views: 620
Reputation: 37146
It's because $key1
is not defined.
You meant to use %{ $file1::hash{$key} }
instead.
Note that if you avoid pre-declaring $key1
, the strict
pragma can catch it at compile-time:
for my $key (keys %file1::hash){
print "$key\n";
for my $key1 (keys %{$file1::hash{$key1}}){
print "$key1\n";
}
}
Message
Global symbol "$key1" requires explicit package name
Upvotes: 6