Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Using Templates

by PyrexKidd (Monk)
on Oct 18, 2010 at 16:02 UTC ( [id://865980]=perlquestion: print w/replies, xml ) Need Help??

PyrexKidd has asked for the wisdom of the Perl Monks concerning the following question:

I have a script that takes several parameters as input, via either CLI or CSV, then it takes values insets them in a template and spits them out into a new file.

Currently the way I am achieving this:

#open CSV for input open my $CSVIN, '<', $file; foreach (<$CSVIN>){ #split CSV into usable format #the real script has about ten variables (my $input1, $input2, $input3, $file_out) = split(/,/,$_); open my $TEMPLATEIN, '<', $path_to_template; open my $FHOUT, '>', $file_out; foreach (<$TEMPLATEIN>){ #search and replace in the template # where $search_pattern = arbitrary identifier #in the template, I've been using: #<<<Variable_1>>> #again I have about twelve $_ =~ s/$search_pattern1/$input1/g; $_ =~ s/$search_pattern2/$input2/g; $_ =~ s/$search_pattern3/$input3/g; print $FHOUT $_; } # end s/r from template close($FHOUT); close($TEMPLATEIN) } # end iteration through CSV

I realize TIMTOWTDI, really what I am asking is what you have found to be the cleanest to read and the most efficient to run.

also any suggestions on "Best Practices" are very welcome

Replies are listed 'Best First'.
Re: Using Templates
by jethro (Monsignor) on Oct 18, 2010 at 16:54 UTC

    There are many excellent templating modules around. If you need to expand your script later to do more complicated things you might regret that you didn't use one of those from the beginning

    Since you have about 12 replacement patterns it might be worthwhile to combine them into one regex. Using the regex switch e and an array to store the input list you could do something like this:

    my @input = split(/,/,$_); my $file_out= pop @input; ... foreach ... ... $_=~ s/<<<Variable_(\d+)>>>/$input[$1]/ge;

    Untested. Also will give out warnings if there are fewer input parameters on the command line than in the file. A solution would be to change regexp line to

    $_=~ s/<<<Variable_(\d+)>>>/if ($1>=@input) {''} else {$input[$1]}/ge;
Re: Using Templates
by samtregar (Abbot) on Oct 18, 2010 at 17:12 UTC
    Virtually any templating system will be faster than that because it will compile the template once and use the compiled version to build the output in the loop. Here's how it would look if you used HTML::Template for example:

    use HTML::Template; my $TEMPLATE = HTML::Template->new_file($path_to_template); open my $CSVIN, '<', $file; foreach (<$CSVIN>) { #split CSV into usable format #the real script has about ten variables (my $input1, $input2, $input3, $file_out) = split(/,/, $_); open my $FHOUT, '>', $file_out; $TEMPLATE->param({$var1 => $input1, $var2 => $input2, $var3 => $input3, # ... }); print $FHOUT $TEMPLATE->output(); close($FHOUT); } # end iteration through CSV

    Faster, and less code too!

    -sam

Re: Using Templates
by kcott (Archbishop) on Oct 18, 2010 at 16:17 UTC

    The code itself looks fine.

    PBP will tell you to:

    • Use for instead of foreach
    • Leave out the parentheses from functions, e.g. use split /,/, $_ instead of split(/,/,$_)
    • And use msx on your regexes

    Update: You should have use strict; and use warnings; at the top of your code. I realise this is just a code fragment and you may have those lines in the complete script - but you did ask :-)

    -- Ken

Re: Using Templates
by sundialsvc4 (Abbot) on Oct 18, 2010 at 19:09 UTC

    Unless I am actually processing millions of files, I care very little for what is thought to be “most efficient.”   But I always care very much for what is“cleanest to read.”

    A very good rule of thumb for this is, “separation of concerns.”   In the code-snippet you show us, it is hard to immediately see how “input processing and preparation” is cleanly separated from “templating and output.”   But if you look at the later reply which shows the use of a templating-engine, it is much more clear.   That’s because the template describes what data is used and how that data is formed into an output string.   I’m really not terribly concerned which templating system you use.

    I am also keenly sensitive to, “how maintainable will this thing be?”   When (not “if”) a change is needed, how quickly and confidently can a total stranger make that change?   How likely is it that an “isolated” change to one aspect of the software will have not-isolated (i.e. “suh-prize! it’s two o’clock in the morning and your beeper’s going off...”) consequences?

Re: Using Templates
by Anonymous Monk on Oct 18, 2010 at 21:03 UTC

    You want to use Template-Toolkit. That's what pretty much everyone uses these days. Only choose something else if you have a compelling reason to do so.

Log In?
Username:
Password:

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

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

    No recent polls found