Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^3: Is there a Perl API (XS) for ->can ?

by chromatic (Archbishop)
on Oct 08, 2021 at 16:51 UTC ( [id://11137359]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Is there a Perl API (XS) for ->can ?
in thread Is there a Perl API (XS) for ->can ?

You have to read between the lines a little bit, but the last large block of text in the EXPORTS section explains why calling isa as a function can give you the wrong results. The same reasoning applies to calling can as a function.

Replies are listed 'Best First'.
Re^4: Is there a Perl API (XS) for ->can ?
by LanX (Saint) on Oct 08, 2021 at 17:20 UTC
    > You have to read between the lines a little bit

    What about the WARNINGS part?

      NOTE: can directly uses Perl's internal code for method lookup, and isa uses a very similar method and cache-ing strategy. This may cause strange effects if the Perl code dynamically changes @ISA in any package.

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

      I think that affects both the method and function forms, but I don't have code that proves it at the moment.

        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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11137359]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-16 20:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found