473,399 Members | 3,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Reading lines from a file and printing variable values

I am creating a web site where I want to have the same menu on each page.

I created a "menu file", and put the html print statements in the file. The menu file contains html code:

<table>
<tr>
<td>
<a href=get_plans_summary.pl?Invoice=$FORM{Invoice}</a>
</td>
</tr>
</table>

Inside of my perl script, I print out each line from the file, including a value for the variable $FORM{Invoice} (which I am getting from a previous link):

$FORM{Invoice} = "2007_08";
$top_menu = "top_menu.txt";

open(line, "$top_menu") || die ("\nERROR - could not open file: $filename\n");
while (<line>)

{

print "$_\n";

}

My problem is that it prints the following:

<a href=get_plans_summary.pl?Invoice=$FORM{Invoice}</a>

and it doesn't translate the variable $FORM{Invoice} to the value that I have set for it earlier.

If I don't have it print from the file, then it works - so I know that the value is being retrieved from the previous html page.

How do I tell it to print from the file and at the same time, translate the value of $FORM{Invoice}?

Thanks
Sep 8 '07 #1
2 2182
KevinADC
4,059 Expert 2GB
I am creating a web site where I want to have the same menu on each page.

I created a "menu file", and put the html print statements in the file. The menu file contains html code:

<table>
<tr>
<td>
<a href=get_plans_summary.pl?Invoice=$FORM{Invoice}</a>
</td>
</tr>
</table>

Inside of my perl script, I print out each line from the file, including a value for the variable $FORM{Invoice} (which I am getting from a previous link):

$FORM{Invoice} = "2007_08";
$top_menu = "top_menu.txt";

open(line, "$top_menu") || die ("\nERROR - could not open file: $filename\n");
while (<line>)

{

print "$_\n";

}

My problem is that it prints the following:

<a href=get_plans_summary.pl?Invoice=$FORM{Invoice}</a>

and it doesn't translate the variable $FORM{Invoice} to the value that I have set for it earlier.

If I don't have it print from the file, then it works - so I know that the value is being retrieved from the previous html page.

How do I tell it to print from the file and at the same time, translate the value of $FORM{Invoice}?

Thanks

Input from files is treated as single-quoted strings, so no variable expansion is done on what looks like perl varibles in the text. It's just treated as literal text.

You can look into using templates, HTML::Template is often sighted as an example of how to use templates with perl scripts. Personally, I find templates to be a bit restriciting and just another layer of code to have to worry about but its pretty much dejur these days as code and content are said to be better seperated from each other. I don't really agree with that except in certain situations.

Another way is to use a "marker" in your html code, could be something like:

<<INVOICE>>

inserted into the place you want to use it in your html code. Then you use a regexp to perfrom the magic:

Expand|Select|Wrap|Line Numbers
  1. $html =~ s/<<INVOICE>>/$FORM{'Invoice'}/;
where $html is the text of the file. Or you can make your text perform an actual expansion of the variable using a regexp with the "e" (eval) option:

Expand|Select|Wrap|Line Numbers
  1. $html =~ s/(\$FORM{'Invoice'})/$1/eeg;

the "g" is added in case there is more than one instance of the variable in the string. If you have multiple variables you need to use a more generic regexp.




.
Sep 8 '07 #2
This is a really great suggestion - and one that is so easy - I can't believe I didn't think of it... I guess that happens after sitting at a computer for 14 hours...

Thanks!
Sep 8 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Suresh Kumaran | last post by:
Hi All, Does anybody know the sytax in VB.NET to write the contents of a multiline text box to a text file? Appreciate help. Suresh
0
by: Luke Airig | last post by:
I am trying to merge two xml files based on common date/time and then write out a tab-delimited xml file with the header record from one of the input files concatenated in front of the merged...
7
by: Daniel Moree | last post by:
I'm working on a program that must first establish if the file exists in the program directory then it must open if for reading, read each line and set the variables then the program goes on about...
2
by: mahurshi | last post by:
I am trying to read a file full of numbers followed by spaces (and then do some cool stuff with it) My input file looks like this 1 0 1 0 1 0 1 1 1 0 0 1 1 0
40
by: googler | last post by:
I'm trying to read from an input text file and print it out. I can do this by reading each character, but I want to implement it in a more efficient way. So I thought my program should read one...
5
by: Kevin Walzer | last post by:
I want to write some variables (user preferences, specifically) to a text file and then read the values from that file. Here is my code to write the data: verbosemodes= """ Detailed = "-vv"...
13
by: swetha | last post by:
HI Every1, I have a problem in reading a binary file. Actually i want a C program which reads in the data from a file which is in binary format and i want to update values in it. The file...
6
by: jcasique.torres | last post by:
Hi everyboy. I trying to create a C promang in an AIX System to read JPG files but when it read just the first 4 bytes when it found a DLE character (^P) doesn't read anymore. I using fread...
2
by: rka77 | last post by:
Hi, I am trying to make a Python2.6 script on a Win32 that will read all the text files stored in a directory and print only the lines containing actual data. A sample file - Set : 1 Date:...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.