in reply to possible to determine the name of a sub from a sub reference?
A dispatch table built around a hash is a quick and easy way to do this in most situations. It gives you a label to match to determine which subroutine to call which can also be logged before the call is made.
use strict; use warnings; sub one { print "one\n"; } sub two { print "two\n"; } sub tres { print "never selected!\n"; } my %dispatch = ( 'one' => \&one, 'uno' => \&one, 'two' => \&two, 'dos' => \&two, 'default' => sub { print "sorry, $_[0] not found.\n"; }, ); my @do_these = qw( one two uno dos tres ); my $log; open $log, '>>', 'example.log' or die "Cannot write to log: $!\n"; foreach my $do_this ( @do_these ) { if ( exists $dispatch{ $do_this } ) { print $log $do_this . "\n"; $dispatch{ $do_this }->(); } else { print $log "default: $do_this not found\n"; $dispatch{ 'default' }->( $do_this ); } }
|
|---|