Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Passing subroutine as argument between packages

by Mateusz (Initiate)
on Nov 30, 2011 at 14:10 UTC ( #940865=perlquestion: print w/ replies, xml ) Need Help??
Mateusz has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,

I have one question about passing subroutines as arguments in packages.

Here is my simple code for question:

* file: FooConfig.pm
package FooConfig; use strict; [ constructor and some functions ] sub important_func { my $self = shift; my $arg = shift; my %hash = ( '1' => 'red', '2' => 'blue', '3' => 'green', ) return $hash{ $arg } if $hash{ $arg }; }

* file: Foo.pm

package Foo; use strict; [ constructor and some functions ] sub somesub_foo { my $self = shift; [...] use FooConfig; my $FooConfig = FooConfig->new; my $ref = \&FooConfig->important_func( '1' ); [ do something with this ] }

Result is: Undefined subroutine &Foo::FooConfig->important_func called at /path_somewhere/Foo.pm line 123

Im trying &$FooConfig, \$FooConfig and many other this without success. :(

Valid usage this function: $FooConfig->important_func, but reference return &PackageName:: before and i got this error. What's wrong with this?

Best regards,

Comment on Passing subroutine as argument between packages
Select or Download Code
Re: Passing subroutine as argument between packages
by Eliya (Vicar) on Nov 30, 2011 at 14:26 UTC
    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->().

      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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (8)
As of 2014-12-19 00:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (69 votes), past polls