Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Changing the name of a function in the call stack

by kikuchiyo (Hermit)
on Nov 19, 2021 at 17:42 UTC ( [id://11138949]=perlquestion: print w/replies, xml ) Need Help??

kikuchiyo has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I've a somewhat involved question.

I use Attribute::Handlers to create function wrappers, quite like as described in this blog post by Gabor Szabo, in the section "Separating Attribute to module".

This works fine, however, I've noticed that these wrapped/replaced functions appear as __ANON__ in the call stack (as shown by the caller function in list context).

Is there a way to give a name to these anonymous subs (preferably to keep the name of the function they've replaced)?

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

Replies are listed 'Best First'.
Re: Changing the name of a function in the call stack
by Corion (Patriarch) on Nov 19, 2021 at 17:51 UTC

    See for example Sub::Name.

    I thought there was a special variable to set the name of the current subroutine, but I don't find it. So Sub::Name is the most likely candidate here.

      Excellent, thanks!

      (I looked at the XS source of that module - wow, that looks extremely complicated.)

        Also have a look at Sub::Util (set_subname()) which is included with Perl since v5.21.4.

        A new module, Sub::Util, has been added, containing functions related to CODE refs, including subname (inspired by Sub::Identity) and set_subname (copied and renamed from Sub::Name). …

Re: Changing the name of a function in the call stack
by choroba (Cardinal) on Nov 19, 2021 at 17:52 UTC
    That's what Sub::Name should do.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: Changing the name of a function in the call stack
by LanX (Saint) on Nov 19, 2021 at 17:51 UTC
    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

      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.
Re: Changing the name of a function in the call stack
by LanX (Saint) on Nov 19, 2021 at 21:40 UTC
    I'm not sure what you attempt to do, but newer Perl support lexical subs, which might solve your problem.

    my sub name { }

    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: perlquestion [id://11138949]
Approved by Corion
Front-paged by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2024-04-25 11:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found