... elsif ( ref($data) eq '' ) { $_[0] = $subref->($data); } ... #### ... if ( ref($data) eq 'HASH' ) { for my $k ( keys %$data ) { if( ref $data->{$k} ) { change_all_elements( $data->{$k}, $subref ); } else { $data->{$k} = subref->($data->{$k}); } } } ... # and the same for arrays