Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^3: Prepocessing perl code / substing variables

by haukex (Archbishop)
on Apr 15, 2019 at 20:31 UTC ( [id://1232608]=note: print w/replies, xml ) Need Help??


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

It is not about parsing perl. It is about replacing %%VARIABLE_NAME%% with some value in the code while installing the script...

If that's really all it's about, then why not just use perl -pie 's/%%VARIABLE_NAME%%/value/g' script.pl? Or s/%%(\w+)%%/$vars{$1}/g? (These are not rhetorical questions)

A search and replace that doesn't parse Perl requires that the search pattern only appears in places where it should be replaced and nowhere else, i.e. it limits what Perl syntax can be used. If that's the case, then one can just as well use e.g. Template::Toolkit (or reinvent that wheel).

As soon as one requires the preprocessing to have even the slightest awareness of Perl syntax, then we're talking about parsing Perl. If you don't believe me, give me an example and I'll give you the counterexample that breaks it ;-)

In the situation you've described, config files or env vars are almost certainly the better solution than editing Perl source code.

Replies are listed 'Best First'.
Re^4: Prepocessing perl code / substing variables
by nataraj (Sexton) on Apr 15, 2019 at 20:49 UTC

    Yes, this is actually a question about how to do s/%%(\w+)%%/$vars{$1}/g in a right way.

    May be my mind is not perl enough, but I consider s/%%(\w+)%%/$vars{$1}/g a hack, and would prefer a module that does s/%%(\w+)%%/$vars{$1}/g.

    May be it is a reason, why there is no such module... 'cause everybody thinks s/%%(\w+)%%/$vars{$1}/g is enough. But I would at least expect missing values reporting... May be some functionality with quotes escaping or something for better values substitution...

      'cause everybody thinks s/%%(\w+)%%/$vars{$1}/g is enough

      Yes, I suspect that is the case...

      But I would at least expect missing values reporting

      warnings about uninitialized variables could cover that, I think.

      May be some functionality with quotes escaping or something for better values substitution...

      Here's where one starts entering the area of reinventing the Template wheel :-)

      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.

      Probably any of the template modules could handle it.

      However, I also think you would be better off using a configuration file. Even using a template to generate the configuration file would be better than messing with the Perl source code. Each container would get a configuration file suited to it's needs.

      Also, by using configuration files, it is far easier to make changes. Without configuration files, the package would have to be uninstalled then re-installed.

        Probably any of the template modules could handle it.

        Thanx. Actually that was the most useful advice. I found a https://metacpan.org/pod/Text::Template::Simple module that do exactly what I need. So there is no need to write it myself and do some strange regexps. Thanks. Actually I do not understand why I did not do that search request myself O_o

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (1)
As of 2024-04-24 13:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found