pileofrogs has asked for the wisdom of the Perl Monks concerning the following question:
Greetings, wise Monks!
How do you get a ref to a method? EG, if I want a ref to Foo->new(), how do I do that? I can get a ref to Foo::new, but that doesn't do what I want. EG:
I want to do this to make my tests simpler. I want to pass a code ref to a run_test() sub.use Foo; my $new = \&Foo::new; my $obj = &$new(); # Bzzzt! without the ->, it doesn't pass the class # name as the 1st arg $new = sub { return Foo->new(@_) }; $obj = &$new(); # does what I want, but is this necessary?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Ref to Method?
by almut (Canon) on Mar 04, 2009 at 19:43 UTC | |
|
Re: Ref to Method?
by Bloodnok (Vicar) on Mar 04, 2009 at 19:13 UTC | |
by chromatic (Archbishop) on Mar 04, 2009 at 19:24 UTC | |
|
Re: Ref to Method?
by Narveson (Chaplain) on Mar 04, 2009 at 19:39 UTC | |
by pileofrogs (Priest) on Mar 04, 2009 at 21:47 UTC | |
|
Re: Ref to Method?
by ikegami (Patriarch) on Mar 04, 2009 at 19:46 UTC | |
|
Re: Ref to Method?
by zwon (Abbot) on Mar 04, 2009 at 18:53 UTC |