in reply to What's your favorite method of passing parameters into a sub?

I use named parameters all the time - it gives you the most flexible interface, and you don't need to look up the order of the parameters. It also makes the calls themself clearer.

But I never use the hashref method to pass in parameters. It's ugly, pointless and just adds punctuation without any gain.

I also like to use local %_ = @_ to put the named parameters in a hash.

Abigail

Replies are listed 'Best First'.
Re: Re: What's your favorite method of passing parameters into a sub?
by jk2addict (Chaplain) on Aug 20, 2002 at 14:55 UTC

    It's ugly, pointless and just adds punctuation without any gain.

    I think I mostly agree with that statement. However, I would also tend to think that passing in 1 hashref vs. 2*n parameters would be more efficient in some situations...mainly if the hashref of opts you pass in will also get forwarded into an internal serializer class method(s) or some such foo.

    Now granted, you could just pass \%args, and the performance difference is probably not measureable so it's all a matter of taste again I guess.

    I agree, it's ugly and pointless on a ->new( {option1=>'on'} ) call, where there is no strict set of parameters, just 'options', but on a class method, I would think it more desireable ->dostuff('param1', {option1=>'on', option2=>'0'}).

    -=Chris

      I think I mostly agree with that statement. However, I would also tend to think that passing in 1 hashref vs. 2*n parameters would be more efficient in some situations...mainly if the hashref of opts you pass in will also get forwarded into an internal serializer class method(s) or some such foo.
      You mean, compared to the overhead of actually calling a sub? You'll have to create a far fetched example to make a significant difference. And there's of course always &func;.
      I agree, it's ugly and pointless on a ->new( {option1=>'on'} ) call, where there is no strict set of parameters, just 'options', but on a class method, I would think it more desireable ->dostuff('param1', {option1=>'on', option2=>'0'}).
      This confuses me. The first one suggests it's a constructor, and constructors often are class methods as well. So why is a hashref ugly in the first example, but more desireble in the second? (For me, they all subs - whether they are used as methods (either class or object) or not).

      Abigail