This is great, but I was confused for a while and tinkered with it. Here's an annotated version:
#! /usr/bin/perl -w
use strict;
my $fname = "aaabbbcccddd";
# Some creepy / characters to prove the quotemeta is doing its job
my $orig = "abc/";
my $repl = "def/";
print "before: $fname\n";
$_ = $fname;
eval sprintf "tr/%s/%s/", map quotemeta, $orig, $repl;
$fname = $_;
print "after: $fname\n";
=for explain
The eval line above was confusing to me at first (and I'm not even
a total newbie). It may help to see it delimited with more
parenthesis:
eval (sprintf ("tr/%s/%s/", map (quotemeta, $orig, $repl)));
From right to left:
The map uses quotemeta as its EXPR and $orig, $repl as its input list
quotemeta is operating on a local $_
The two variables sprintf is expecting are in the list output from map
eval is evaluating the resulting string made by sprintf. The tr/// is
operating on $_
=cut
|