Re: Indirect method invocation
- From: fxn@xxxxxxxxxxx (Xavier Noria)
- Date: Wed, 27 Jul 2005 17:55:41 +0200
On Jul 27, 2005, at 17:21, Jan Eden wrote:
My other problem is that the cookbook quotes (something like) the following code as an example for storing method names:
my %actions = (
display => $item->display(),
move_picture => $item->move_picture($item->{id}, $item-> {parameters}->{position}),
commit => $item->commit()
);
Are you sure? $item->display() returns whatever the display() method returns, which might be a coderef, but looks unlikely.
Since you can't get a reference to a method (bound to an instance) as you do with regular subroutines, the technique normally is to wrap them in an anonyomus closure like this:
package Foo;
sub new { bless {}, shift }
sub method { print "Moo!" }package main;
my $foo = Foo->new;
my %actions = (foo => sub { $foo->method(@_) } );
$actions{foo}(); # prints "Moo!"-- fxn
.
- References:
- Indirect method invocation
- From: Jan Eden
- Indirect method invocation
- Prev by Date: Re: Help: automated email forwarding similar to .forward
- Next by Date: Re: login shell?
- Previous by thread: Re: Indirect method invocation
- Next by thread: login shell?
- Index(es):
Relevant Pages
|
|