473,662 Members | 2,390 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reading lines from a file and printing variable values

19 New Member
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?Invo ice=$FORM{Invoi ce}</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.t xt";

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?Invo ice=$FORM{Invoi ce}</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 2191
KevinADC
4,059 Recognized Expert Specialist
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?Invo ice=$FORM{Invoi ce}</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.t xt";

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?Invo ice=$FORM{Invoi ce}</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
widevision7
19 New Member
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
12726
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
2071
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 detail records in the output file. The version I have is close to what I need except it concatenates two sets of header records in front of each merged detail record and the first two lines in the file and the last line is blank. Can someone please...
7
2553
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 it's buisness. My problem is all the resources I have found aren't very clear on these things. All of them open the file then check to see if the stream is open. Well, the problem with using file.open("filename.dat", ios::in | ios::binary) is...
2
4215
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
4509
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 line at a time and print it out. How can I do this? I wrote the code below but it's not correct since the fscanf reads one word (terminating in whitespace or newline) at a time, instead of reading the whole line. #include <stdio.h> void...
5
2629
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" Basic = "-q" """
13
3695
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 consists of structures of type---- struct record { int acountnum; char name; float value;
6
4224
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 function. Here a few lines: char *sAnv; .... sprintf(file_a, "%s/anverso.jpg", strDir);
2
4092
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: 10212009 12 34 56 25 67 90 End Set ******** Set: 2 Date: 10222009
0
8343
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8856
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8762
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8545
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6185
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4179
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4347
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2762
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1747
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.