in reply to passing hashes to a function
I would handle things a little differently, but this assumes you're more comfortable with references than you might be at this point. Untested:
Instead of dereferencing the reference and copying the resulting hash (what you did with $food), this accesses the underlying hash through the reference. If you're concerned about potential modification of the hash in the subroutine, be aware that this technique could allow that.sub print_hash { my $hash_ref = shift; my $value; foreach $key (keys %$hash_ref) { $value = $hash_ref->{$key}; print "fruit: $key is $value\n\n"; } }
For more details, see perlref, as it describes the %$ and $->{} syntaxes in sufficient detail. (Dereferencing)
|
|---|