• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 146
  • Last Modified:

Reading in and printing a form with variables

I am having some trouble with the following.  I have to
read in a file which contains colon delimited information, and split it into variables, ie:  first name, last name, subscription length, cost.  I have accomplished this portion.  I also have to read in a "form letter" that contains scalar vars ie: $firstname, $lastname, $cost, etc etc.  I have not been able to figure out how to read in this file and then print the variables that I have already read in to this form letter, to then be printed to a file called something like form_letter.$lastname.  Any suggestions?
  • 2
  • 2
1 Solution
I think the easiest solution is to write your form-letter into some file (let's call it "blank_form.txt") and keep it in the same directory as the script.  This blank letter will look like this:


Your final cost is YYYYYYYYY, due on the date ZZZZZZ.


Now, in your script open this file and slurp into a variable, such as with:
open(IN, '<blank_form.txt');
my $form;
while (<IN>) { $form .= $_; }

# Now just replace the dummy strings you put in the form
$form =~ s/XXXXXXXXX/$first_name $last_name/;
$form =~ s/YYYYYYYYY/$cost/;
$form =~ s/ZZZZZZZZZ/$date/;
$form =~ s/QQQQQQQQQ/$my_name/;

# And write this final form to a file:
open(OUT, '>filled_form.txt');
print $form;

# P.S. THere are more elegant ways to do this, but this
#      way is damned easy to program, relatively efficient
#      and easily maintainable in a production environment
The previous solution by itatsumaki will work fine.  Here's another (more compact) alternative...

Dear $first_name $last_name,

Your final cost is $cost, due on the date $date.


open IN, 'form.txt' or die "could not open: $!";
open OUT, ">form.$last_name" or die "could not write: $!";
while (<IN>) {
    print OUT;
close OUT;
close IN;
Just a note on wilcoxon's solution: it uses symbolic (soft) references, which can be problematic if you do
use strict;
use strict 'refs';
True.  I meant to point that out, but was running short of time.  For a fairly simple program (couple hundred lines of code max), I'd use the symbolic refs (possibly with "use strict" and "no strict 'refs'" (easier than doing "use strict qw(blah blah blah)")).  For a longer program, I would use something more like itatsumaki's original suggestion.

You can do some very interesting things with symbolic references.  At a previous job, I implemented a email templating system where non-programmers entered an email template into the database.  If they needed new tags for it, they could either program a code snippet or get a programmer's help with the code snippet (which was later eval'd).  It was a very extensible system that (mostly) required no programming knowledge to use (but could get a little slow on complex emails (mostly due to the evals)).
manovitAuthor Commented:
Thanks for the great help.  I had never even broached the idea of reading the form into an array and running a series of sed's on it.  That worked extremely well.  Thanks again - and sorry for the delay on the comments!

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now