my $acc; my %handler = ( 1 => sub { $acc = $messagebin{$file}{1}; }, 11 => sub { ... }, ); if (my $code = $handler{+$subkey}) { $code->($subkey); # I found it convenient to have the dispatch value # available in the dispatch handler } else { warn "Unhandled subkey value $subkey"; };