printing html with perl

Discussion in 'Perl Misc' started by Guy, May 7, 2009.

  1. Guy

    Guy Guest

    For those who use Perl to generate HTML pages...

    I just started reading CGI Programming with Perl by Guelich Gundavaram and
    Birznieks, and I learned that you can generate HTML codes with, such
    as the following but it sounds strange to me because you would have to learn
    a new HTML syntax.

    print $q->start_html(-title=> "My Site" );

    I have learned about here documents which uses << followed by a token. This
    appears easier to me.

    Are there any advantages to using in this case?
    Guy, May 7, 2009
    1. Advertisements

  2. Guy

    Nathan Keel Guest

    It depends on what you're doing. I think most tag names are similar or
    the same, but just use normal heredocs's and HTML how you do now. The
    CGI module can save some time and coding and do things for you and be
    consistent, but that doesn't mean it's necessary or better.
    Nathan Keel, May 7, 2009
    1. Advertisements

  3. In my hands, this generates a DOCTYPE, an <html>, a <title></title>,
    plus some stuff about a content type and character set, and a <body>.
    So it seems to cover quite a bit of HTML syntax that I've never bothered
    to learn. How important all that stuff is, I don't know.

    Also, any special characters in your -title string will automatically
    get HTML encoded. Whether this is a good thing depends on what you want
    to happen, but I generally consider it a good thing.
    It appears orthogonal to me. You can use << with nothing but a print,
    or you can use it inside a start_html.

    print $q->start_html(-title=> << END);
    My site

    Xho Jingleheimerschmidt, May 7, 2009
  4. Guy

    Scott Bryce Guest

    I write a lot of CGI in Perl, but I never use to generate HTML.
    If you want to generate HTML pages in Perl, you would be much better off
    using a templating system, such as HTML::Template. Using here documents
    to generate HTML can get pretty messy pretty fast.
    Scott Bryce, May 7, 2009

  5. A very nice example of why I never use for generating HTML. :)

    I never use for generating HTML because it obscures (for me
    anyway) the program's output.

    i.e. It makes debugging harder.

    (errr, IE makes debugging harder too, but that's a different thing :)

    I had cookie(), redirect() and header() in mind when I wrote that.
    Tad J McClellan, May 7, 2009
  6. A. Sinan Unur, May 7, 2009
  7. Guy

    ccc31807 Guest

    I frequently do this, which enables variable interpolation:
    print qq(<p>She said, "Let me help polish the Polish silver."</p>);

    I also frequently put my HTML in an HTML module and call it by passing
    my variables as arguments, so that my might have a function
    like this:
    sub print_submit_button
    my ($class, $value, $name) = @_;
    print qq(<input type="submit" name="$name" value="$value" class="$
    onmouseout='this.className="${class)_out";' />);

    I call it like this:
    print_submit_button('big_red_button','Hit Me Again','do_what');

    ccc31807, May 8, 2009
  8. use HTML::Template or


    A. Sinan Unur <>
    (remove .invalid and reverse each component for email address)

    comp.lang.perl.misc guidelines on the WWW:
    A. Sinan Unur, May 8, 2009
  9. Guy

    Ben Bullock Guest

    Ben Bullock, May 9, 2009
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.