Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^3: How to use a variable in tr///

by dmorelli (Scribe)
on Apr 08, 2005 at 14:52 UTC ( #446029=note: print w/replies, xml ) Need Help??


in reply to Re^2: How to use a variable in tr///
in thread How to use a variable in tr///

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://446029]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (9)
As of 2018-07-19 15:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (411 votes). Check out past polls.

    Notices?