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

Can an anonymous subroutine determine its own code reference at runtime?


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
If I understand your problem, I can solve it! Of course, the same can be said for you.

Replies are listed 'Best First'.
Re: Anonymous coderefs
by diotalevi (Canon) on Aug 28, 2003 at 19:58 UTC

      Perfect, thanks diotalevi. I remembered doing it for named subs, but not anonymous ones.


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
      If I understand your problem, I can solve it! Of course, the same can be said for you.

Re: Anonymous coderefs
by dragonchild (Archbishop) on Sep 02, 2003 at 15:34 UTC
    Wouldn't
    my $x; $x = sub { # Do stuff. $x->(); # Do more stuff };
    do the trick? That's something BrowserUk offers as a way to write recursive subs ...

    Update: As BrowserUk can't remember offering this advice, I confess that it might have been tye, or demerphq, or any number of the smart people on this board. I think it was that I read too many response from the British One that I had him on the brain. :-)

    ------
    We are the carpenters and bricklayers of the Information Age.

    The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

      I do? When? Where? What else have I forgotten? Have I got a hole in my brain? Brain? What's a brain? What? Huh?


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
      If I understand your problem, I can solve it! Of course, the same can be said for you.

Re: Anonymous coderefs
by ihb (Deacon) on Mar 11, 2005 at 13:24 UTC

    It's cheating, but Sub::Recursive kind of does that with very little effort for you.

    use Sub::Recursive; #my $foo = sub { ... }; my $foo = recursive { ... };
    and inside the block $REC is a reference to the subroutine.

    ihb

    See perltoc if you don't know which perldoc to read!