Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Reading from/writing to files with variables substitutions

by Qukz (Novice)
on Mar 28, 2013 at 07:26 UTC ( #1025875=perlquestion: print w/ replies, xml ) Need Help??
Qukz has asked for the wisdom of the Perl Monks concerning the following question:

Hello.

I have some boilerplate text files with some variables I'd like to substitute. I'd then like to create new files with the appropriate substitutions based on those; for example (one of the boilerplate text files):

Hello $FIRST_NAME. I'm writing on behalf of $COMPANY...

How would I go about using those files to create new files with the substituted values, so the new file would then be

Hello James. I'm writing on behalf of SoftLayer...

The following works

my $outfile = "new.txt"; open (OUTFILE, ">> $outfile") || die "Can't open $outfile\n"; print OUTFILE "\n"; print OUTFILE "Hello $FIRST_NAME.\n"; print OUTFILE "\n"; print OUTFILE "I'm writing on behalf of $COMPANY...";

But I can't figure out how to do the above while keeping the boilerplate text files seperate from the script itself and still printing the substituted variables rather than the literal names of the variables.

Any help would be greatly appreciated.

Comment on Reading from/writing to files with variables substitutions
Select or Download Code
Re: Reading from/writing to files with variables substitutions
by Anonymous Monk on Mar 28, 2013 at 07:34 UTC
Re: Reading from/writing to files with variables substitutions
by aitap (Deacon) on Mar 28, 2013 at 07:36 UTC
    So, you have a template and you want to process it? Template::Toolkit should be useful, if you don't mind changing your template syntax from $substitution to [% substitution %].
    Sorry if my advice was wrong.
Re: Reading from/writing to files with variables substitutions
by 2teez (Priest) on Mar 28, 2013 at 08:29 UTC

    Hi Oukz,
    Really, aitap's wisdom for me is the way to go, however, if I may suggest using here-document like so:

    use warnings; use strict; my $first_name = "James"; my $company = "SoftLayer"; my $letter = << "COMPANY_LETTER"; Hello $first_name. I'm writing on behalf of $company .... COMPANY_LETTER my $outfile = "new.txt"; open my $fh, '>', $outfile or die "Can't open file: $!"; print $fh $letter; close $fh or die "can't close file: $!";

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: Reading from/writing to files with variables substitutions
by BillKSmith (Chaplain) on Mar 28, 2013 at 15:24 UTC

    Use a hash and a regex with the /e option.

    #!perl -p use strict; use warnings; #Usage: perl script.pl boiler.txt >outfile.txt BEGIN{ use vars qw( %hash $pattern ); %hash = ( FIRST_NAME => 'James', COMPANY => 'SoftLayer', ); $pattern = join '|', keys %hash; } s/\$($pattern)/$hash{$1}/ge;
    Bill
Re: Reading from/writing to files with variables substitutions
by perlaintdead (Scribe) on Mar 28, 2013 at 23:58 UTC

    suggestion: it's best if you use 3 arg opens

    like:

    open(FILE, ">>", "filename.txt");

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2014-12-26 23:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (176 votes), past polls