Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^2: Preferred technique for named subroutine parameters?

by AnomalousMonk (Archbishop)
on May 22, 2009 at 20:34 UTC ( [id://765740]=note: print w/replies, xml ) Need Help??


in reply to Re: Preferred technique for named subroutine parameters?
in thread Preferred technique for named subroutine parameters?

... 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.

Replies are listed 'Best First'.
Re^3: Preferred technique for named subroutine parameters?
by akho (Hermit) on May 22, 2009 at 20:43 UTC
    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)
        Which is kinda useless if you're trying to pass this reference into a function.
        I've no possibility to test it ATM, but I think the point is that the hash-refs are passed unchanged into the function. Your right that after my %args = @_; it will be stringified, but at least they can still be found unchanged in @_.

        The crucial problem about stringified refs is that normally you have no means to tell which ref corresponds to HASH(0x225398), but in this case you have at least the possibility from within the function to search @_ (or to create a second hash) to find it out!

        Cheers Rolf

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-04-20 00:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found