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.