#!/usr/bin/perl use strict; use warnings; use Carp; { my %actions = ( one => \&one, two => \&two, three => \&three ); sub dispatch { # detect early all invalid arguments my @invalid = grep {!exists($actions{$_})} @_; if (@invalid) { local $" = ', '; croak("invalid action(s): (@invalid)"); # or the death spell of your choice } # else { for (@_) { # call $actions{$_}->(...), for example: $actions{$_}->("<<$_>>"); } # } } } # stub subs generator for exemplification only BEGIN { eval 'sub ' . $_ . q{ { local $" = ', '; print "sub [@{[(caller 0)[3]]}] with args: (@_)\n"; }} for qw/one two three/; } dispatch(qw/one two three/); dispatch(qw/three two one kaboom silence/);