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 CGI.pm, 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 CGI.pm in this case?
    Guy
     
    Guy, May 7, 2009
    #1
    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
    #2
    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
    END


    Xho
     
    Xho Jingleheimerschmidt, May 7, 2009
    #3
  4. Guy

    Scott Bryce Guest

    I write a lot of CGI in Perl, but I never use CGI.pm 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.

    http://search.cpan.org/~samtregar/HTML-Template-2.9/Template.pm
     
    Scott Bryce, May 7, 2009
    #4

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

    I never use CGI.pm 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
    #5
  6. A. Sinan Unur, May 7, 2009
    #6
  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 HTML.pm might have a function
    like this:
    sub print_submit_button
    {
    my ($class, $value, $name) = @_;
    print qq(<input type="submit" name="$name" value="$value" class="$
    {class}_out"
    onmouseover='this.className="${class}_over";'
    onmouseout='this.className="${class)_out";' />);
    }

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

    CC
     
    ccc31807, May 8, 2009
    #7
  8. use HTML::Template or Template.pm

    Sinan

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

    comp.lang.perl.misc guidelines on the WWW:
    http://www.rehabitation.com/clpmisc/
     
    A. Sinan Unur, May 8, 2009
    #8
  9. Guy

    Ben Bullock Guest

    s/cute/evil/g;
     
    Ben Bullock, May 9, 2009
    #9
    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.