Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^3: Preferred technique for named subroutine parameters?

by akho (Hermit)
on May 22, 2009 at 20:43 UTC ( #765745=note: print w/ replies, xml ) Need Help??


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

Try using a reference as a hash key (or check out the relevant question in perlfaq4).


Comment on Re^3: Preferred technique for named subroutine parameters?
Re^4: Preferred technique for named subroutine parameters?
by AnomalousMonk (Monsignor) on May 22, 2009 at 21:06 UTC
    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.
        Which is kinda useless if you're trying to pass this reference into a function.
        Well, if you're trying to pass a reference into a function and also have it be the name of an argument, yes.

        I wonder if you mean to pass a named parameter having a value that is a reference? Now, that seems to work pretty much as advertised:

        >perl -wMstrict -MData::Dumper -le "sub func { my %args = %{ $_[0] }; print Dumper \%args; print $args{foo}->{bar}; } func({ foo => { bar => 'baz' } }); " $VAR1 = { 'foo' => { 'bar' => 'baz' } }; baz
      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

        But surely the point is that if you pass a reference (to whatever, and however generated) as the value of a named parameter in the  func({ foo => [ 1, 2 ] }); scheme, then there is no problem whatever.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (14)
As of 2014-07-25 14:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (172 votes), past polls