Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Two interesting problems reguarding reading files . . .

Status
Not open for further replies.

youthman

Programmer
Apr 28, 2004
49
US
Good morning Perl Masters!

I have two interesting questions. For some time now I have been using a website script that reads from a database and places the information on the customers screen using an HTML template. (Different templates for different sites.) First the script reads a config file line by line and splits the line at an = sign. Then is processes each line with an "if statment."
The script then loads the sites template and uses the search and replace function ($template =~ s/SCRIPT_URL/$site_url/gi) to locate the keyword for the script and replace it with the site's url. Then it pulls the SQL database (of 192 Columns) and does the same thing with the data. ($template =~ s/COLUMN1/$row[0]/gi; $template =~ s/COLUMN2/$row[1]/gi; ...etc. to the end of 192 Columns) Then prints $template to the screen with it is done.

My first question is this:
Is there a way to process the config file without using a series of if statments (Making it faster)?? Right now the config file looks like this:
site_name=This Sites Name
site_url=http://www.sitesurl.com
This is getting to be a large file and it is taking a long time to process it.

My second question is similar:
Is there a better way of replacing the information in the HTML. Right now if the script finds 10 rows in the database, it has to process the 192 columns of the database into the HTML 10 times before it prints all that to the HTML template then to the customers screen. Is there a more efficient way of doing this?

Thanks for your help.
 
Don't use a template? i.e. get Perl to output the HTML for you... then you can simply use placeholders (not sure if this is the correct terminology!?)

i.e.

Code:
print <<HTML

You could insert [b]$row[0][/b] here and [b]$row[32][/b] here etc...

And yet another [b]$row[77][/b] here...

HTML


Kind Regards
Duncan
 
Alternately to Template::Toolkit, you could look into HTML::Template as well and decide which you like more.

To me, they're both great, but I prefer to use HTML::Template on sites that are not going to implement all the things you can use in Template Toolkit.

In the end, of course, it's all up to which you like best.

- Rieekan
 
Don't listen to my solution - Trojan & Rieekan are probably right. I am a nightmare in that i never use modules - and i probably should

I imagine Template::Toolkit is the way to go


Kind Regards
Duncan
 
duncdude . . . You crack me up! As a lurker, I have come to respect your opinions in this group (along with many others) and find most of your answers to be simpler than many others. Thanks for the input.
To everyone, I have looked at both these modules, and at least on the surface, they appear to basically do the same thing that my script is already doing. Do you believe that with the amount of data that I am placing on the users screen these modules are going to be faster? That is the main concern. . . . SPEED!

Thanks again for your help.
 
Well i am touched... thank you very much!

If you can avoid regex's i would imagine it would speed up substantially. I couldn't honestly say by how much... maybe you could try just one (large template) in the new fashion i suggested above? Your database query is grabbing the variables you need and the HERE document example above would make for a very quick replacement - as they would just appear where you need them to? I could have missed the point though. However, if you can avoid the regex's...

Let us know how you get on

Good luck, and thanks again for your kind comments!


Kind Regards
Duncan
 
OK, if speed is what you want, can you post some code please.
The obvious issue that springs to mind for me is your cluster of "if" tests.
You may be able to reduce them to a single hash lookup.
If you show me what you're doing, I'll look to simplifying and speeding it up for you.


Trojan.
 
here documents are prone to erros in that the syntax has to be so exact. I prefer to use the qq operator for printing:

print qq~
You could insert $row[0] here and $row[32] here etc...
And yet another $row[77] here...
~;

then you don't have to worry about the end of line/string terminator being where it has to be when using HERE document type printing. Plus it seems using qq has a small speed advantage over HERE docs for reasons unknown to me.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top