'While executing the following snippet. I could see an error Global symbol "$colors" requires explicit package name. Key and value has to be printed

Trying to print fruits followed by their colors using a for loop.

use strict; 
my %colors = (  apple      => 'red',
                orange     => 'orange',
                watermelon => 'green',
                grapes     => 'blue',
                rest       => 'pink' );
for (keys %colors) {    
   print("color of  $_  is   $colors($_)\n"); 
}


Solution 1:[1]

$colors($_) must be $colors{$_}

Accessing hash values requires the use of { }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Miguel Prz