Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I'm just learning/reading about Inheritance, very nice. But what was not explained was what you can do with the returned value from can(),like:
$a = A->new ; $b = $a->can(test) ; print $b ; # ????
Output
    CODE(0x8169abc)

Any suggestions ?

Thanks in advance
Luca

Replies are listed 'Best First'.
Re: what does the retval of can() do ?
by marto (Cardinal) on Feb 15, 2006 at 14:09 UTC
    Luca,

    The return value from $a->can(test) checks if there is a method called 'test' within the class or object. If it does then a reference to the sub is returned, else undef is returned.

    This can be used to conditionally invoke a method:

    $dead->dance if $dead->can("dance");

    Update: as derby mentions check the documentation :)

    Martin
      what can you do with the reference of the sub ?
        You can dereference it (and call it). What did you expect?

        Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
        How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
          A reply falls below the community's threshold of quality. You may see it by logging in.
Re: what does the retval of can() do ?
by derby (Abbot) on Feb 15, 2006 at 14:05 UTC