On Mon, 9 Apr 2001 11:38:54 +0900, Nobumi Iyanaga wrote:

>I have a short question about the "tr" command.  Is it impossible to use 
>variables in the tr command, in this way? :
>
>$str =~ tr/$to_find/$replace_with/;

No. You have to use eval, as John Gruber already wrote.

But I prefer not doing the eval STRING every single time you need to
call it, but instead, wrap it in a closure:

        sub gen_tr {
            my($to_find,$replace_with) = @_;
            return eval <<"#END_OF_SUB#";
            sub {
                foreach(\@_) {
                    tr/\Q$to_find\E/\Q$replace_with\E/;
                }
            }
#END_OF_SUB#
        }

        *custom_tr = gen_tr("aiueo", "hijkl");

        $str = "aiueokakikukeko";
        custom_tr($str);
        print $str, "\n";
__END__

-- 
        Bart.

Reply via email to