So over the past few months, I've tried to be very conscious about to writing better, cleaner code. I think I've improved a great deal but one spot that I frequently encounter that I'm still not quite sure how to handle is HTML generation. So take this line, for example, which makes me feel dirty:

my $load_js = "<script>function load_js(uri) { var script = document. +createElement('script'); script.src = uri; document.head.appendChild( +script); }</script>\n";

I know it's not good practice to mix content with code. But what's the best practice for a line like this? I didn't want to spread it over several lines as that would use up space. A HERE document is just plain ugly to look at in code and eats up space. And I didn't want to introduce any new modules or a template just to generate a few lines of javascript. I vaguely recall others recommend using __DATA__ for this kind of stuff but I guess I just don't see what the advantage to doing that is and it makes you have to go down to the end of the file to see what's happening.

Maybe what I'm going in the example code is perfectly fine. But I have a nagging feeling it isn't. If someone can set me straight and offer suggestions, I'd appreciate it.

PS to the haters: If you think it's in the best interests of the Perl community to tell me how dumb and worthless I am, go for it! I'm here for you.

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest";
$nysus = $PM . ' ' . $MCF;
Click here if you love Perl Monks


In reply to Best practices: Generating HTML content by nysus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.