# in init phase: my $commands = join "|", keys %myhash; my $command_regex = qr/^($commands)\s*(.*)$/i; # in input loop: while() { if ( m/$command_regex/o ) { my $command = lc $1; $myhash{ $command }->( $2 ); } else { $myhash{"error"}->( $_ ); } }