Perl (Scripting) - Resolve "Experimental keys on scalar is now forbidden"

by
Jeremy Canfield |
Updated: July 11 2024
| Perl (Scripting) articles
Let's say something like this is being returned.
Experimental keys on scalar is now forbidden at /path/to/example.pl line 10.
Type of arg 1 to keys must be hash or array (not private variable) at /path/to/example.pl line 10, near "$food) "
Execution of /path/to/example.pl aborted due to compilation errors.
I got this when attempting to loop over a variable hash that contained key/value pairs. In this example, the variable named $food is a hash containing key/value pairs.
foreach my $key (keys $food) {
print "$key\n";
}
For example, my $food variable contains a hash of key/value pairs like this.
$VAR1 = {
'fruit' => 'apple',
'veggy' => 'pepper'
};
I resolved this by updating my for each loop to have %{$food}
foreach my $key (keys %{$food}) {
print "$key\n";
}
Did you find this article helpful?
If so, consider buying me a coffee over at