Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Changing the name of a function in the call stack

by LanX (Saint)
on Nov 19, 2021 at 17:51 UTC ( [id://11138951]=note: print w/replies, xml ) Need Help??


in reply to Changing the name of a function in the call stack

IIRC was the book "Perl Hacks" describing something similar, but I'm not sure if it was only a debugging aid.

edit

see "Hack #57"

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

  • Comment on Re: Changing the name of a function in the call stack

Replies are listed 'Best First'.
Re^2: Changing the name of a function in the call stack
by LanX (Saint) on Nov 19, 2021 at 20:15 UTC
    No XS needed:
    use strict; use warnings; use Carp; sub generate { my ( $name ) = @_ ; return sub { local *__ANON__ = $name # M A G I C if $name; carp "***sub called***"; } } sub super { my $not_named = generate(); $not_named->(); my $named = generate('HulaHoop'); $named->(); } super();

    -*- mode: compilation; default-directory: "d:/tmp/pm/" -*- Compilation started at Fri Nov 19 21:13:52 C:/Strawberry/perl/bin\perl.exe -w d:/tmp/pm/name_anno_sub.pl ***sub called*** at d:/tmp/pm/name_anno_sub.pl line 12. main::__ANON__() called at d:/tmp/pm/name_anno_sub.pl line 20 main::super() called at d:/tmp/pm/name_anno_sub.pl line 28 ***sub called*** at d:/tmp/pm/name_anno_sub.pl line 12. main::HulaHoop() called at d:/tmp/pm/name_anno_sub.pl line 25 main::super() called at d:/tmp/pm/name_anno_sub.pl line 28 Compilation finished at Fri Nov 19 21:13:53

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

      This is inferior to Sub::Name for two reasons:

      • you have to put the magic line inside the anon sub
      • all other anon subs down the call stack are renamed too, which is misleading
      use strict; use warnings; use Carp; sub foo { my $bar = sub { carp "*** inner anon sub called***"; }; $bar->(); } sub generate { my ( $name ) = @_ ; return sub { local *__ANON__ = $name if $name; carp "***sub called***"; foo(); } } sub super { my $not_named = generate(); $not_named->(); my $named = generate('HulaHoop'); $named->(); } super();
      Still, it's good to know that it's possible to do it in pure perl.
        > This is inferior to Sub::Name for two reasons:

        well, I was aware of the side effects, but you said in the OP

        > create function wrappers,

        so I supposed you are going to automate the wrappers, hence all anon subs would be treated by your attribute handlers and you could de-localize the name before calling the wrapped sub.

        Anyway, I wouldn't be surprised if the mechanics of Sub::Name were possible without XS and just using methods from the B backend module

        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://11138951]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found