moggs has asked for the wisdom of the Perl Monks concerning the following question:

Now here's a really easy question (it's even multiple choice):

When using perl to create the HTML I commonly use lines like

print qq~<ul> <li>blurb 1</li> <li>blurb 2</li> </ul>\n~;

However, I've recently found myself writing it differently

print qq~<ul>\n~; print qq~<li>blurb 1</li>\n~; print qq~<li>blurb 2</li>\n~; print qq~</ul>\n~;

The output is the same, but the source code is so much neater. So my question is: Does this effect PERL's efficiency in any negative form?

Thanks for your assistance on this trivial point

Replies are listed 'Best First'.
Re: print qq once or many times
by BrowserUk (Patriarch) on Dec 14, 2010 at 01:28 UTC

    For neatness I much prefer:

    print <<END; <ul> <li>blurb 1</li> <li>blurb 2</li> </ul> END

    As for performance, for those tiny pieces of output, they will get buffered locally and not get transmitted until you close STDOUT (which usually means when you exit the script. Unless you've disabled buffering, which if your interested in performance you probably shouldn't have. For such small amount of output you'd be very hard push to detect any significant difference anyway.

    Then the next step is:

    print <DATA>; ... __DATA__ <ul> <li>blurb 1</li> <li>blurb 2</li> </ul>

    And before you know it you're into the whole Template::Toolkit behemoth.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: print qq once or many times
by GrandFather (Saint) on Dec 14, 2010 at 01:25 UTC

    Does the "efficiency" really matter? If you have no immediate compelling reason to be concerned write it whichever way is most maintainable. Personally I'd probably use HTML::Template or some similar technology and remove the HTML from the code entirely.

    True laziness is hard work
Re: print qq once or many times
by Anonymous Monk on Dec 14, 2010 at 01:08 UTC
    Now here's a really easy question (it's even multiple choice):

    For the easy questions, use Benchmark 'cmpthese';

Re: print qq once or many times
by moggs (Sexton) on Dec 17, 2010 at 00:26 UTC

    Thanks for the comments and the reminder to keep it all maintainable.

    I did run benchmarking which showed there is essentially no difference between the different approached (or at least no difference for less than a million lines of code). The answer I got was:

    (warning: too few iterations for a reliable count) Rate Name1 Name2 Name1 999999999999999872/s -- 0% Name2 999999999999999872/s 0% --

    I figure this means they're all the same :o)

    Thanks again, Moggs

      I figure this means they're all the same :o)

      It also means you did not use cmpthese( -3 , ... you should share the benchmark :)