Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^5: Prepocessing perl code / substing variables

by roboticus (Chancellor)
on Apr 15, 2019 at 21:04 UTC ( [id://1232613]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Prepocessing perl code / substing variables
in thread Prepocessing perl code / substing variables

nataraj:

It may just be a bit too simple to bother with a module:

$ cat pm_1232611.pl use strict; use warnings; my %replacements = qw( FOO fubar BAZ bazoom BIM Kaboom! ); my @strings = ( "what a %%FOO%% for a %%BAZ%%", "Eenie, meenie, minie, %%BIM%%", "The quick %%RED%% fox..." ); for my $str (@strings) { print "INPUT: <$str>\n"; my @vars = $str =~ m/%%(\w+)%%/g; for my $var (@vars) { if (exists $replacements{$var}) { $str =~ s/%%$var%%/$replacements{$var}/g; } else { print "Warning, no replacement for %%$var%%\n"; } } print "OUTPUT: <$str>\n\n"; } $ perl pm_1232611.pl INPUT: <what a %%FOO%% for a %%BAZ%%> OUTPUT: <what a fubar for a bazoom> INPUT: <Eenie, meenie, minie, %%BIM%%> OUTPUT: <Eenie, meenie, minie, Kaboom!> INPUT: <The quick %%RED%% fox...> Warning, no replacement for %%RED%% OUTPUT: <The quick %%RED%% fox...>

...roboticus

When your only tool is a hammer, all problems look like your thumb.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1232613]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-25 16:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found