in reply to passing subroutine args as a hash: why not?
I generally prefer a hash ref, because I find it easier to work with.
sub hash { my %in = @_; . . . } sub hashref { my $in = shift; my %in = %{ $in }; . . . } hash( foo => 'bar' ); hashref({ foo => 'bar' });
At first glance, it looks like there is slightly more code to do a hashref compared to a plain hash. However, IIRC, mis-formatting the hash in the subroutine call will be a run-time error, whereas mis-formatting the hashref will be compile-time. IMHO, if you get to choose, compile-time errors are better than run-time errors. Additionally, hashrefs let you sprinkle in a few positional params, if you so choose.
sub hashref_and_positional { my $param1 = shift; my $param2 = shift; my $in = shift; my %in = %{ $in }; . . . } hashref_and_positional('foo', 'bar', { baz => 'blah' });
----
I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
-- Schemer
Note: All code is untested, unless otherwise stated
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: passing subroutine args as a hash: why not?
by revdiablo (Prior) on Jun 06, 2003 at 05:18 UTC |