in reply to getting subroutine names

I agree with Merlyn that just giving names is the easiest way, by far. However, if you really want to get into some deep, dark perl voodoo, check this out:

#!/usr/bin/perl use warnings; use strict; sub test1 {1}; sub test2 {2}; sub test3 {3}; sub test4 {4}; sub findsym { no strict 'refs'; my ($pkg,$ref) = @_; foreach my $sym ( values %{$pkg."::"} ) { return *$sym{NAME} if *{$sym}{CODE} && *{$sym}{CODE} == $ref; } } my @tests = (\&test1,\&test2,\&test3,\&test4); foreach my $subref (@tests) { my $name = findsym(__PACKAGE__,$subref); print STDERR "Test $name\n"; print $subref->() . "\n"; }

I recently discovered this kind of thing poking around in the guts of Attribute::Property and Attribute::Handlers and thought it was pretty cool. I've never even seen a print reference to the {NAME} property of a typeglob.

-xdg

Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Replies are listed 'Best First'.
Re^2: getting subroutine names
by linuxfan (Beadle) on Jan 13, 2005 at 18:41 UTC
    This solution is even better. Gets me the name of the subroutine!!

    Thanks a lot :)