You could use prototypes (see perlsub, caveats here). Consider:
#!/usr/bin/perl sub getInfo ($\%) { my $scalar = shift; my $hash = shift; print "scalar = '$scalar'\n"; print "hash = $hash\n"; print "$_ => $hash->{$_}\n" for keys %$hash; } my %hash = (foo => 'bar', baz => 'quux'); my $scalar = 'string'; getInfo($scalar,%hash); __END__
output:
scalar = 'string' hash = HASH(0x81677a4) baz => quux foo => bar
This has the advantage of throwing an error at compile time if the wrong arguments are passed in:
my @array = qw(foo bar baz); getInfo($scalar, @array); Type of arg 2 to main::getInfo must be hash (not private array) at foo +.pl line 16, near "@array)"
--shmem
you mean Passing, not Parsing :-)
update: inserted "You could" and caveat :-)
_($_=" "x(1<<5)."?\n".q·/)Oo. G°\ /
/\_¯/(q /
---------------------------- \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
In reply to Re: Passing a hash into a sub
by shmem
in thread Parsing a hash into a sub
by RaginElmo
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |