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

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

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/ 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,

Replies are listed 'Best First'.
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.


        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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://940865]
Approved by Eliya
[choroba]: I wouldn't want to maintain it in the current form
[erix]: ok, but mainainability comes after it has been made to work :)
[erix]: maybe I should have cobbled together a more simple example
[erix]: the main question is whether repeating groups get captured. I thought they did but it looks they do not.
[erix]: or they probably get overwritten when the repeating is done

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2018-01-19 08:39 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (216 votes). Check out past polls.