in reply to Finding the name of the target of a coderef

This question gets asked from time to time. The usual answer is, why do you want to do that? It is a strange thing to want, and when you consider that something may be called by many names, or even no name at all, it is nearly pointless as well.

  • Comment on Re: Finding the name of the target of a coderef

Replies are listed 'Best First'.
Re^2: Finding the name of the target of a coderef
by Forsaken (Friar) on Apr 14, 2005 at 23:11 UTC
    The main reason I want to do this is because I'm working on a small script that executes a certain subroutine with certain arguments after x amount of time, a timed event so to say. In order to facilitate ease of use I was hoping to find a way to provide a list of all timed events currently in the queue, with the subroutines they're pointing to, the arguments and the amount of time left till execution.

    Remember rule one...

      I would solve that by passing a label along with the coderef when I added the timed event to the queue. Something along the lines of:

      add_to_queue(foo => \&foo, [ @fooargs ], $footime); add_to_queue(bar => \&bar, [ @barargs ], $bartime); dump_queue(); { my @queue; sub add_to_queue { my ($label, $code, $args, $time) = @_; push @queue, [ $label, $code, $args, $time ]; } sub dump_queue { for (@queue) { my ($label, $code, $args, $time) = @$_; print "Will execute $label with @$args at $time\n"; } } }

      Of course, your exact calling conventions will likely be different, but this is just to give you the general idea.