Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^3: How to identify the package of a subroutine given only a reference to it

by LanX (Saint)
on Feb 01, 2022 at 02:32 UTC ( [id://11141005]=note: print w/replies, xml ) Need Help??


in reply to Re^2: How to identify the package of a subroutine given only a reference to it
in thread How to identify the package of a subroutine given only a reference to it

> So now have an object which contains the package name and the generated subroutine

If you want to go that way you can also choose to bless the code ref directly.

Objects don't need to be hashes in Perl.

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

  • Comment on Re^3: How to identify the package of a subroutine given only a reference to it

Replies are listed 'Best First'.
Re^4: How to identify the package of a subroutine given only a reference to it
by drclaw (Acolyte) on Feb 02, 2022 at 00:51 UTC

    I'm partial to array backed objects where applicable. But I've never actually tried a blessed code ref... until 5 minutes ago after reading your reply! It might be a good solution to my application.

    Out of interest, do you have insight on how perl knows to call the underlying code ref and not a method that may or may not be in the package? An extra lookup/check must be done when called?

    #eg $code_ref_obj->();#calls code ref directly $code_ref_obj->a_method(); #Call a method on the object
    Thanks again for you comments
      > do you have insight on how perl knows to call the underlying code ref

      I'm not sure I understand, your demo nailed it.

      • $code_ref->(ARGS) without method name is de-referencing the code-ref.
      • $obj->method_name(ARGS) with method name is looking up the "blessed" package inside the structure of the scalar and will try to resolve the method there.

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

        My question wasn't very directional... I was wanting to know the relative performance of a blessed code ref vs a unblessed code ref

        So I put together a little benchmark comparing:

        1. calling a code ref directly
        2. blessing a code ref and calling it directly
        3. blessing a code ref and calling another method in the object

        Blessing the code ref does have a performance impact when using it as a code ref compared to an unblessed code ref. However it is cheap compared to calling a method on the object

        use strict; use warnings; use feature ":all"; use Benchmark qw<cmpthese>; my $count=$ARGV[0]//1; package SUB_TEST { sub new { bless sub {1}, __PACKAGE__} sub method { 1 } } my $obj=SUB_TEST->new(); my $sub=sub {1}; cmpthese($count, { blessed=>sub {$obj->()}, method=>sub{$obj->method()}, sub=>sub{$sub->()} } );
        Output:
        Rate method blessed sub method 18939394/s -- -30% -39% blessed 27173913/s 43% -- -12% sub 30864198/s 63% 14% --

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11141005]
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: (8)
As of 2024-04-25 11:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found