Re: Indirect method invocation



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


.



Relevant Pages

  • Re: another way to shoot yourself in the foot?
    ... Of course you're passing a reference, since limited types are always passed by reference (since Ada 95). ... package Wrap is ...
    (comp.lang.ada)
  • Re: Creating a class that is "readonly" for some classes?
    ... Make a check on each entrance into the object ... to wrap create another object that wraps the main object that can write to ... least a reference to its parent/owner so that it can make a comparison. ... > class Worker1: WorkerBase ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: testing whether an object exists
    ... This is a lot of stuff for me to wrap my brain around, ... >> If I set this object and reference at the module level, ... > Private WithEvents objXL As Excel.Application ... > Private WithEvents objWkbk As Excel.Workbook ...
    (microsoft.public.vb.general.discussion)
  • Re: VB.NET SNMP example
    ... but I can't add the reference. ... Write the code that talks to SNMP in managed C++ and wrap ... > all calls in .NET objects. ...
    (microsoft.public.win32.programmer.networks)
  • RE: FQN from references
    ... And in that routine i want to know the full name of that coderef. ... > To: Gupta, Sharad ... >> How do i know the full name of a variable from its reference??. ...
    (perl.beginners)