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


In reply to Using Templates by PyrexKidd

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.