Whenever I loop over a hash by its keys, and then printing each value, I get an "use of of uninitialized value in concatenation (.) or string..." warning. Even though the hash is clearly initialized up front. The output I want is printed, but I'd still like to know why this results in a warning, especially as accessing a value directly (outside of a loop) works without warnings.
#!/usr/bin/perl
use warnings;
use strict;
my %fruit = ();
%fruit = ('Apple' => 'Green', 'Strawberry' => 'Red', 'Mango' => 'Yellow');
#works
print "An apple is $fruit{Apple} \n";
#gives warnings
foreach my $key (%fruit)
{
print "The color of $key is $fruit{$key} \n";
}
#also gives warnings
foreach my $key (%fruit)
{
my $value = $fruit{$key};
print "$value \n";
}
Consider the above code. I guess perl sees a difference between the first print and the second print. But why? Why is there a difference between retrieving the value of a hash outside of loop and retrieving the value of a has inside of a loop?
Thanks!