Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.

  • $obj->baz()

    tells me

    BAZ::baz at d:/tmp/pm/tst_can.pl line 32

  • my $meth =$obj->can('baz'); $obj->$meth();

    tells me

    FOO::baz at d:/tmp/pm/tst_can.pl line 10.

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-25 23:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found