Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Python, Lua, Tcl, C and C++ training - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Java, Ruby, MySQL and Linux / Tomcat systems
Interpolating text from a file

Posted by Pat (Pat), 16 April 2004
In a Perl CGI, I'm trying to generate an HTML page using some literal text and a file containing needed HTML text.  

An example of the file material might be:  
'The mouse ate the $x' written without interpolation so the $x appears in the file.

I open the file for input and read in the segment using code like:
open FILEHANDLE, "<msg_file.txt";
$segment = <FILEHANDLE>;

Then I make an assignment:  $x = "cheese";

Finally, I write out the HTML segment with a statement like:
   print "<br> $segment";

The result has $x where I had expected "cheese" to appear.  

How can I do better?

Thanks in advance,

Posted by admin (Graham Ellis), 16 April 2004
Hi, Pat .... good question this one.  It doesn't work the way you want it to because for most (non-programmer) users, the idea of a program that changes anything starting with a $ in their data files would be pretty dis-concerting  

Try this:

open (FH,"dollarinfile");
$hot = 35;
$line = <FH>;
print $line;
$line =~ s/\$(\w+)/$$1/eg;
print $line;

and the results:

earth-wind-and-fire:~/corr grahamellis$ perl
The temperature today is $hot degrees
The temperature today is 35 degrees
earth-wind-and-fire:~/corr grahamellis$

Explanation .... I've used regular expressions to find all simple variable names hidden in the line of text and capture the variable name to $1.  I've replaced the variable name with the result of executing the code (the e switch) $$1 - $ really means "contents of", so this is the contents of the variable which is named in the captured $1 variable.

As the front of Larry Wall's "camel book" says ... "there's more than one way to do it".  Perhaps other posters will follow up with their own favourite solutions!

Posted by Pat (Pat), 16 April 2004

Thanks for the reply -- and the solution.  

Last night in bed I realized my problem was just a "find and replace" situation.  But you've outlined the solution very nicely, and I'll use it as the base for my work.

Thanks for taking the time to decode my question and supply the "right" answer.

You mention Larry Wall.  He developed Perl while he worked at the Jet Propulsion Laboratory.  I worked there during his era, interfaced with him occasionally, but had no idea his product would become so popular.  That was years ago, but I'm sure he's still a nice guy.

Thanks again,

Posted by admin (Graham Ellis), 16 April 2004
Pat ... you brought a smile to my face.  I've had the pleasure of meeting with Larry; still very much a "nice guy" yet those words seem inadequate somehow.


P.S. Please don't quote my answer as being "the right one" - I like it, but "there's more than one way"

This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2017: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho