English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I am not talking about creating a subroutine.

I simply want to automatically substitute text from other files into perl scripts at certain places before I run it.

Is there a simple way of doing this?

(Note, I cannot do this with subroutines, since I want to substitute the beginning of a loop from one place, the middle from another, and the end from yet another)

2006-06-05 17:15:19 · 3 answers · asked by Anonymous in Computers & Internet Programming & Design

3 answers

If I get your question right, this is what you want.
Your perl file is actually some kind of a template where there are placeholder variables whose values will be known at run time from another file, and you need some way to replace the correct values in their correct places.

I once had a similar requirement and I resolved it by writing another program (you can write this one in perl, I chose java) that takes the template you have and create a new perl script with the placeholder variables replaced with those in the other file.

You need to execute this newly generated file now.

2006-06-05 19:37:42 · answer #1 · answered by Neil 5 · 0 1

in the text files that are replacements, have the text formatted the way you want , whatever, ready to be inserted . .and

in the 'master' perl program that does the calling use the unix/linux cat command within backticks like

$the_file_contents = `cat /home/httpd/www/reaplace1.txt`;

then voila, all the text is in there ready to be manipulated like any other scalar variable

2006-06-06 14:08:49 · answer #2 · answered by bow4bass 4 · 0 0

In a word, 'eval'.

I imagine your program would end up looking a bit like this:

my $execText = "";
$/ = undef;
$execText .= <"top.pl">;
$execText .= <"middle.pl">;
$execText .= <"bottom.pl">;

eval {$execText};

This wouldn't be a smart way to solve most problems. If you want to execute code in another file, use the traditional constructs 'require' and 'use'. You'll be glad you did.

2006-06-10 21:27:35 · answer #3 · answered by wwarner 1 · 0 0

fedest.com, questions and answers