in reply to named_arguments in subroutine
When you say you passed a hash, do you mean you called the routine with a hash as a parameter? and if so did you call it like
my $value=mySub(%hash); or my $value=mySub(\%hash;)
Example call below
#! /usr/bin/perl use strict; use warnings; my %hash=qw(fruit apple dairy cheese cereal oats); sub whatis{ my $hash=shift; for my $category (keys %{$hash}){ print "$hash->{$category} is a $category\n"; } } whatis(\%hash);
Or do you mean that the subroutine returns a hash and you wish to access a value of this hash by it's key? Horrible example below
Update: added code examples#! /usr/bin/perl use strict; use warnings; sub foodCategories{ my %hash=qw(fruit apple dairy cheese cereal oats); return \%hash; } sub whatis{ for my $category (keys %{foodCategories()}){ print &foodCategories->{$category}," is a $category\n"; } } whatis();
|
|---|