in reply to Re^3: use strict refs unless you can't figure out the syntax
in thread use strict refs unless you can't figure out the syntax
For some definition of 'better':
use strict; use warnings; package Something; sub Logic { my ($param) = @_; print "Heh, Logic works$param\n"; } package main; my @names = qw(Logic Illogic); my %dispatch = map {$_ => Something->can ($_)} @names; for my $subName (@names, 'missing') { if (! exists $dispatch{$subName}){ print "Don't know about '$subName'\n"; } elsif ($dispatch{$subName}) { $dispatch{$subName}->('!'); } else { print "Can't call '$subName'\n"; } }
Prints:
Heh, Logic works! Can't call 'Illogic' Don't know about 'missing'
|
|---|