Gossamer Forum
Home : General : Perl Programming :

parsing file contents

Quote Reply
parsing file contents
Hello again, first off thanks to everyone that has shown me help in my never ending desire to learn perl.

I am wondering how to parse the contents of a html file.

If i have:
open (PROFILEHTML, "$profilehtml");
@profile = <PROFILEHTML>;
close PROFILEHTML;

This will just read in the html file specified in the script, but what if I want to parse the file so that I can change certain variables that are specified in the script into the parsed html file before displaying it to the server.

Can someone tell me what a simple command would be to do this with the above example.

Thanks
Harrison


"I've got if's pretty good, but that's about it"
Quote Reply
Re: parsing file contents In reply to
Without knowing what you're trying to parse, it's pretty hard to tell you.

You can go through each line of the file by using

Code:

foreach $line (@profile) {
do something;
}
but I'm not sure what your do something would be. Smile

JPD
http://www.jpdeni.com/dbman/
Quote Reply
Re: parsing file contents In reply to
Hi JPDeni,

Basically what happens now is this,
A user logs into our system, and the script pulls in the html page menu.html

Inside of this menu.html page are variables like $username and $city and $state, all of these variables reside in the database.

If we put the html coding for the menu.html page all the variables like $username and $city etc are changed to what is in the mysql database,

But what we would like to do is to read in and parse the entire menu.html file and change all the variables of $whatever to what they are in the database.

Like I said, if I put the html coding of the entire html file into the print <<eof; section then all the variables are translated properly, but we want to pull in a "template" file that resides on the server (menu.html) and have it translate all the $variables without having to manually insert the whole pages html into the script itself.

This would mean that we could just plunk up the html page to the server, let the menu.cgi page pull in the html from the menu.html page and translate any variables.

I thought this would be easy with using
open (PROFILEHTML, "$profilehtml");
@profile = <PROFILEHTML>;
close PROFILEHTML;

but then I found out that is just bringing in the html page and not parsing the actual page to accomplish what we want from it.

So again, I am lost as to how to accomplish this.

Thanks for your time
Harrison


"I've got if's pretty good, but that's about it"
Quote Reply
Re: parsing file contents In reply to
Your code for bringing the file into memory is fine. Each line is put into an element of an array, right? Then you need to look at each element of the array and do something with it.

I think I'm probably over my head here, now, though. Smile

JPD
http://www.jpdeni.com/dbman/
Quote Reply
Re: parsing file contents In reply to
Thanks, I know I'm way over my head here and could use a hand digging myself out of this pitt I've dug myself into.

Harrison


"I've got if's pretty good, but that's about it"
Quote Reply
Re: parsing file contents In reply to
Try this:

#!/path/2/perl -w

print "Content-type: text/html\n\n";

## Open an html file, replace $variables with a value

open(critter,"<an_ordinary_file.htm");
while(<critter>) {
# replace all $variables found
~s/\$(\w+)/${$1}/g;
# print if not empty to standard out
print $_ if $_ ne "";
}
close(critter);

# ~s/\$(\w+)/${$1}/g; from Perl Cookbook page 17, O'Reilly

Good Luck Conrad