Reading in and printing a form with variables

Posted on 2003-02-21
Medium Priority
Last Modified: 2010-03-05
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?
Question by:manovit
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2

Accepted Solution

Itatsumaki earned 200 total points
ID: 7995138
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
LVL 26

Expert Comment

ID: 7995689
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;

Expert Comment

ID: 7995808
Just a note on wilcoxon's solution: it uses symbolic (soft) references, which can be problematic if you do
use strict;
use strict 'refs';
LVL 26

Expert Comment

ID: 7997705
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)).

Author Comment

ID: 8053805
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question