in reply to Passing hashes as arguments to a sub?
Only scalars can be passed as arguments, so either need to pass a reference (like in the first and second snippet), or flatten the hash into a list of scalars (like in the third snippet).
One usually passes a reference to the hash which saves Perl from copying the entire hash:
If you don't with to work with references, you could use an alias:sub function { my ($hash_ref) = @_; foreach (keys %$hash_ref) { ... } } function(\%hash);
Or you could create a copy of the hash if it's the last argument:sub function { our %hash; local *hash = \$_[0]; foreach (keys %hash) { ... } } function(\%hash);
sub function { my %hash = @_; foreach (keys %hash) { ... } } function(%hash);
|
|---|