Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: passing subroutine arguments directly into a hash

by IraTarball (Monk)
on Oct 20, 2001 at 07:19 UTC ( #120201=note: print w/replies, xml ) Need Help??


in reply to passing subroutine arguments directly into a hash

If I understand you right you're looking for something like...
@list = qw (one two three); %args = ( arg1 => 'string', arg2 => 5, arg3 => \@list ); sub1(%args); sub sub1 { my %args = ( arg1 => 'default', arg2 => 0, arg3 => undef, @_ ); print "$_ => $args{$_}\n" for keys %args; }
For me this prints out
arg1 => string arg2 => 5 arg3 => ARRAY(0x1a72f84)

This allows you to pass in the arguments as a hash, provide defaults in the subroutine, and override them with arguments passed in. This is right out of 'Effective Perl Programming' by Joeseph Hall and Randal Schwartz.

Ira,

"So... What do all these little arrows mean?"
~unknown

Replies are listed 'Best First'.
Re: Re: passing subroutine arguments directly into a hash
by drewbie (Chaplain) on Oct 20, 2001 at 08:50 UTC
    sub sub1 { my %args = ( arg1 => 'default', arg2 => 0, arg3 => undef, @_ ); }
    I personally prefer this approach because it 1) tells me what parameters I am expecting and 2) it allows me to set defaults. Yes, it takes up a lot of space visually, but who the heck cares since it makes the code that much more readable. :-)
Re: Re: passing subroutine arguments directly into a hash
by George_Sherston (Vicar) on Oct 20, 2001 at 15:32 UTC
    Hah! I finally understood something! Probably something I shd have understood a while ago... namely that a hash in array context is an array. So When I do
    @list = qw (one two three); %args = ( arg1 => 'string', arg2 => 5, arg3 => \@list ); sub1(%args); sub sub1 { print "$_\n" for @_; }
    I get
    arg1 string arg2 5 arg3 ARRAY(0x17656b4)
    Obvious when you think about it. I mention it here more to fix it in my own mind than in the faint hope of enlightening others, who are probably way ahead of me. But it does make sense for me of a lot of stuff I've been doing on trust, not 100% knowing why it works... like this way of passing arguments to HTML::Template.

    George Sherston
      (well, technically its a list.... but thats for another day)

      Another "a-ha!" moment is just one more step away. The '=>' operator is really just a glorified comma. It will quote simple barewords on the LHS, but thats the only difference. Consider the following:

      my @a = (1 => 2 => 3 => 4); print "$_\n" for @a;
      So, the somewhat unfamiliar:
      (arg1=>$blah, arg2=>$blah2)
      is really just a very familiar four element list:
      ( 'arg1' , $blah , 'arg2' , $blah2 )
      dressed up with some syntactic sugar. (note the need to quote 'barewords' when using commas)

      With this in mind, take another look at your hash assignment and named argument passing.....

      -Blake

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://120201]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (9)
As of 2019-05-27 11:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you enjoy 3D movies?



    Results (156 votes). Check out past polls.

    Notices?