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

Re: Passing subroutine as argument between packages

by Eliya (Vicar)
on Nov 30, 2011 at 14:26 UTC ( #940869=note: print w/ replies, xml ) Need Help??


in reply to Passing subroutine as argument between packages

Valid usage this function: $FooConfig->important_func, but reference return &PackageName:: before

I'm not sure I understand what your problem is.  As it seems, you appear to have figured out that the correct way to call the method would be $FooConfig->important_func( ... ). So where/when are you getting the &PackageName:: you don't want?

Upon rethinking, maybe you want something like this

my $ref = sub { $FooConfig->important_func( '1' ) };

which would 'bind' the object instance and the method parameter to the coderef, so that you could call it later on as $ref->().


Comment on Re: Passing subroutine as argument between packages
Select or Download Code
Replies are listed 'Best First'.
Re^2: Passing subroutine as argument between packages
by Mateusz (Initiate) on Nov 30, 2011 at 18:13 UTC

    Thank you for you answer,

    I want ask you about one more thing: in this method, how pass eventually arguments for subroutine $FooConfig->important_func?

    I checked $ref()->('a' ), $ref()('a'), $ref->( 'a' ), $ref->()('a'), $ref->()->('a') or like this, and didn't work.

      Given:

      my $ref = sub { $FooConfig->important_func( '1', @_ ) };

      ... then you can write:

      $ref->( 'more', $args, %here );

      ... though be cautious and understand how the function reference passes its arguments to the inner function.


      Improve your skills with Modern Perl: the free book.

        Simple @_ is solution - now works fine!

        Thanks for very helpfull hints.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (11)
As of 2015-07-31 02:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (274 votes), past polls