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

Re: Preferred technique for named subroutine parameters?

by akho (Hermit)
on May 22, 2009 at 19:12 UTC ( [id://765730]=note: print w/replies, xml ) Need Help??


in reply to Preferred technique for named subroutine parameters?

This seems to be a hot topic today!

Both methods are widely used, so you are on your own — sometimes one way makes more sense than the other; some people have preferences.

I like flat lists — less parentheses, and you can use non-string keys if you need to (or: someone who extends your module can add non-string stuff without breaking all pre-existing code).

Or you could support both.

Replies are listed 'Best First'.
Re^2: Preferred technique for named subroutine parameters?
by AnomalousMonk (Archbishop) on May 22, 2009 at 20:34 UTC
    ... you can use non-string keys if you need to (or: someone who extends your module can add non-string stuff without breaking all pre-existing code).
    I don't understand this point. Isn't it always possible to use 'non-string' (actually non-bareword) keys as long as they are appropriately disambiguated? Anyone who is aware of the calling convention of your module (as they surely must be to extend the module or to use it in the first place) can easily avoid this pitfall.

    The classic example:
    You have a constant  KONSTANT (defined with the  use constant ... pragma) that you want to use as a key.
    Then just disambiguate  KONSTANT as the function call it really is:
        func({ KONSTANT() => 'foo' });

    Leaving aside a multitude of ambiguities arising from confusion about the precedence of the  , (comma) and  => operators versus other operators, it's hard to imagine another realistic example in which this problem would arise.

      Try using a reference as a hash key (or check out the relevant question in perlfaq4).
        If you are referring to How can I use a reference as a hash key?, I don't see your point.

        Inside an anonymous hash or in a 'flat' list, a hash reference (or any reference) will just get stringized away if you try to use it as a hash key (unless you invoke some other magic).

        >perl -wMstrict -le "sub func { my %args = @_; print keys %args } func({ bar => 'baz' } => 'foo'); func({ fee => 'fie' } , 'foe'); " HASH(0x225398) HASH(0x225398)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (7)
As of 2024-03-28 16:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found