The Implementation 

while(length($input) > 0) {
    if(($char, $rest) = $input =~ $nibble_good_chars) {
        $output .= $char;
    }
    else {
        ($char, $rest) = $input =~ /^(.)(.*)$/s;
        $output .= $byte_map->{$char};
    }
    $input = $rest;
}