in reply to Invoke a method on an object

The easiest way is to do this:

$obj->$method(@args)

But you can also call the code reference you got back from ->can:

my $method = $obj->can($method_name); $method->( $obj, @args );

As $method is not bound to $obj, you have to supply $obj as the first parameter.

Replies are listed 'Best First'.
Re^2: Invoke a method on an object
by thedi (Acolyte) on Jan 13, 2009 at 11:26 UTC
    Thanks, thats all I need

    Best regards

    Thedi
Re^2: Invoke a method on an object
by pobocks (Chaplain) on Jan 13, 2009 at 15:32 UTC

    Since I believe he is talking about $method being a string, wouldn't the first version need no strict 'refs', and a leading ampersand?

    for(split(" ","tsuJ rehtonA lreP rekcaH")){print reverse . " "}print "\b.\n";

      No:

      perl -Mstrict -le "sub P::p{print 'p'};my $p=bless {},'P';my $m='p';$p +->$m;"

        I acknowledge that this works, and runs under "use strict"; but how is $m not a symbolic reference? I'm a little confused here.

        for(split(" ","tsuJ rehtonA lreP rekcaH")){print reverse . " "}print "\b.\n";