Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Efficiency/Speed question... (files or formats?)

by Spidy (Chaplain)
on Oct 26, 2004 at 22:12 UTC ( #402818=perlquestion: print w/ replies, xml ) Need Help??
Spidy has asked for the wisdom of the Perl Monks concerning the following question:

In the interest of efficiency while using CGI, would it be better for me to have HTML tempate files that I read in, replace variables inside of, and then print out, or to just use formats that contain HTML code?

Thanks,
Spidy

Comment on Efficiency/Speed question... (files or formats?)
Re: Efficiency/Speed question... (files or formats?)
by chromatic (Archbishop) on Oct 26, 2004 at 22:21 UTC

    This is a complicated question.

    Efficiency of speed, development time, maintainability, ease of use, or what, in particular?

    How efficient is your current approach in any or all of the above ways that matter? How efficient does it need to be? Can you consider other approaches if they lead to dramatic improvements in one or more of the criteria above?

Re: Efficiency/Speed question... (files or formats?)
by pg (Canon) on Oct 26, 2004 at 22:29 UTC

    Most likely, use a template system will cost a little bit more execution time, however to fulfill a HTTP request, you have to spend time for lots of other activities, two big ones would be: network communication, and database access (if there is any).

    Instead of looking at the time the templating system use, you better look at the percentage of time used by the templating system, compare to the entire cycle of fulfilling the request.

    More importantly, as chromatic pointed out, how easily the application can be developed and maintained is very important, actually more important. With a reasonablly good templating system, most likely you can develop the application in a short time, with a clearer face, and the maintainability is improved.

Re: Efficiency/Speed question... (files or formats?)
by jarich (Curate) on Oct 27, 2004 at 00:51 UTC
    I'm tempted to say neither. In fact I do. Don't do either of those two methods.

    Designing, writing, testing and using your very own yet another templating system is hugely inefficient. Likewise doing the same with formats doesn't help a lot either.

    If you need things to go fast perhaps you should look at CGI::Fast or mod_perl. That'll cut down the execution time a lot.

    Back to the question at hand, however. I would recommend the good practice of using someone else's already well designed, written and tested templating system. For example HTML::Template or Template::Toolkit depending on which one best suits your needs.

    Formats are almost a thing of the past (thankfully) and I can say with certainty that you'll appreciate the joys of using templates, sooner or later. You want to use templates, it's a good habit to get into for when you later have to share a project with a graphic designer.

    Hope this helps,

    jarich

Re: Efficiency/Speed question... (files or formats?)
by Cody Pendant (Prior) on Oct 27, 2004 at 06:04 UTC
    I said this ages ago, but I feel it's still valid.

    There's a process you go through when working with Perl and HTML, unless you're really lucky, and it's like this:

    1. Using hundreds of horrible print statements, all of which neeed to have all the quotes escaped
    2. Using "print qq" instead
    3. Using HERE documents or subs
    4. Using your own templating system
    5. Throwing your own templating system away and using HTML::Template
    I'm proud to say that I have finally got to stage five. And I'm only now realising how many very smart, efficiency-minded extra options HTML::Template has.


    ($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss')
    =~y~b-v~a-z~s; print
Re: Efficiency/Speed question... (files or formats?)
by TedPride (Priest) on Oct 27, 2004 at 12:40 UTC
    If the template is very small, probably the most efficient method is to print directly:
    print <<OUT; First line! This line has a $var in it. Second line... Thanks for reading this! OUT
    If your template is large but has only a few insertion points, you can read the file into a string and use index or rindex:
    use strict; use warnings; my ($handle, $text, $insert); open($handle, 'template.txt'); read($handle, $text, 10000); close($handle); $insert = index($text, '<!-- INSERT -->'); print substr($text, 0, $insert); print 'My inserted data goes here!'; print substr($text, $insert);
    Beyond that, a formal templating system or module is probably best, or you can always use PHP, which is designed for embedding in pages:
    Blah blah blah I'm inserting a variable here: <?php echo $var; ?> And again here: <?php echo $another; ?>
    I still like using PHP for simple things.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://402818]
Approved by atcroft
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2014-09-20 11:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (158 votes), past polls