OK I hacked a little demo with three class FOO > BAR > BAZ with equally named methods

I overrode ->can in the lower two by just delegation it to the SUPER and ignoring the methods in the same class. (dunno if that even makes sense)

The demo shows that calling UNIVERSAL::can($obj,$sym) ignores the overriding ->can methods for the good and the better, it depends what you want.

use strict; use warnings; use Data::Dump qw/pp dd/; package FOO { sub foo { warn __PACKAGE__."::foo";}; sub bar { warn __PACKAGE__."::bar";} sub baz { warn __PACKAGE__."::baz";} }; package BAR { our @ISA = qw/FOO/; sub bar { warn __PACKAGE__."::bar";} sub can { my ( $self,$sym ) = @_ ; warn "Calling can in ".__PACKAGE__." for ".Data::Dump::pp $sel +f; FOO->can($sym); } }; package BAZ { our @ISA = qw/BAR/; sub baz { warn __PACKAGE__."::baz";} sub can { my ( $self,$sym ) = @_ ; warn "Calling can in ".__PACKAGE__." for ".Data::Dump::pp $sel +f; BAR->can($sym); } sub new { my ( $self,@args ) = @_ ; bless {@args} , __PACKAGE__; } }; package main; use Test::More; my $obj = BAZ->new(name=>"OBJ"); for my $sym (qw/foo bar baz/) { diag "****** Trying can($sym)"; diag "... as ->meth\n"; my $meth = $obj->can($sym); $obj->$meth() if $meth; diag "... as sub()\n"; my $sub = UNIVERSAL::can($obj,$sym); $obj->$sub() if $sub; is($meth,$sub,"same for $sym"); } done_testing;

-*- mode: compilation; default-directory: "d:/tmp/pm/" -*- Compilation started at Sun Oct 10 20:52:31 C:/Strawberry/perl/bin\perl.exe -w d:/tmp/pm/tst_can.pl # ****** Trying can(foo) # ... as ->meth Calling can in BAZ for bless({ name => "OBJ" }, "BAZ") at d:/tmp/pm/ts +t_can.pl line 36. Calling can in BAR for "BAR" at d:/tmp/pm/tst_can.pl line 22. FOO::foo at d:/tmp/pm/tst_can.pl line 8. # ... as sub() FOO::foo at d:/tmp/pm/tst_can.pl line 8. ok 1 - same for foo # ****** Trying can(bar) # ... as ->meth Calling can in BAZ for bless({ name => "OBJ" }, "BAZ") at d:/tmp/pm/ts +t_can.pl line 36. Calling can in BAR for "BAR" at d:/tmp/pm/tst_can.pl line 22. FOO::bar at d:/tmp/pm/tst_can.pl line 9. # ... as sub() BAR::bar at d:/tmp/pm/tst_can.pl line 18. not ok 2 - same for bar # Failed test 'same for bar' # at d:/tmp/pm/tst_can.pl line 67. # got: 'CODE(0x6c5730)' # expected: 'CODE(0x67f920)' # ****** Trying can(baz) # ... as ->meth Calling can in BAZ for bless({ name => "OBJ" }, "BAZ") at d:/tmp/pm/ts +t_can.pl line 36. Calling can in BAR for "BAR" at d:/tmp/pm/tst_can.pl line 22. FOO::baz at d:/tmp/pm/tst_can.pl line 10. # ... as sub() BAZ::baz at d:/tmp/pm/tst_can.pl line 32. not ok 3 - same for baz # Failed test 'same for baz' # at d:/tmp/pm/tst_can.pl line 67. # got: 'CODE(0x6c57d8)' # expected: 'CODE(0x26cbc28)' 1..3 # Looks like you failed 2 tests of 3. Compilation exited abnormally with code 2 at Sun Oct 10 20:52:32

UPDATE

I think overriding ->can is a very tricky business.

so overriding ->can should only be used to "publish" NEW dynamic methods via AUTOLOAD and not trying to hide static ones.

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery


In reply to Re^6: Is there a Perl API (XS) for ->can ? by LanX
in thread Is there a Perl API (XS) for ->can ? by NERDVANA

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.