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. Advertising

  2. Guy

    Nathan Keel Guest

    Guy wrote:

    > 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


    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. Advertising

  3. Guy wrote:
    > 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" );


    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.

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


    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

    Guy wrote:
    > 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 write a lot of CGI in Perl, but I never use CGI.pm to generate HTML.

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


    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. Keith Keller <-francisco.ca.us> wrote:
    > On 2009-05-07, Tad J McClellan <> wrote:
    >>
    >> I always use CGI.pm for parsing form arguments.
    >>
    >> I never use CGI.pm for generating HTML.

    >
    > There are some cute features of CGI.pm.


    > print $cgi->li([1,2,3,4]);



    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 often use CGI.pm for other things.

    >
    > There isn't much else to CGI.pm, is there?



    I had cookie(), redirect() and header() in mind when I wrote that.


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
    Tad J McClellan, May 7, 2009
    #5
  6. Tad J McClellan <> wrote in
    news::

    > Keith Keller <-francisco.ca.us> wrote:
    >> On 2009-05-07, Tad J McClellan <> wrote:
    >>>
    >>> I always use CGI.pm for parsing form arguments.
    >>>
    >>> I never use CGI.pm for generating HTML.

    ....
    >
    >>> I often use CGI.pm for other things.

    >>
    >> There isn't much else to CGI.pm, is there?

    >
    > I had cookie(), redirect() and header() in mind when I wrote that.


    I really like http://search.cpan.org/~andya/CGI-Simple-1.109/

    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 7, 2009
    #6
  7. Guy

    ccc31807 Guest

    On May 6, 8:43 pm, "Guy" <> wrote:
    > For those who use Perl to generate HTML pages...


    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. ccc31807 <> wrote in news:4da2b0ce-1cd0-4378-a817-
    :

    > On May 6, 8:43 pm, "Guy" <> wrote:
    >> For those who use Perl to generate HTML pages...

    >
    > 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";' />);
    > }


    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

    On Wed, 06 May 2009 20:48:32 -0700, Keith Keller wrote:

    > There are some cute features of CGI.pm. For example, you can feed
    > list-like elements, like li(), an arrayref, and it'll generate all the
    > HTML for you:
    >
    > print $cgi->li([1,2,3,4]);
    >
    > prints
    >
    > <li>1</li> <li>2</li> <li>3</li> <li>4</li>


    s/cute/evil/g;
    Ben Bullock, May 9, 2009
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gary

    Printing from HTML/Perl

    Gary, Nov 6, 2003, in forum: Perl
    Replies:
    9
    Views:
    545
    GIMME
    Nov 18, 2003
  2. Jon B
    Replies:
    7
    Views:
    7,496
    =?Utf-8?B?SmFzb25DaG9p?=
    Jan 30, 2006
  3. Aqua
    Replies:
    1
    Views:
    122
    Rick Nakroshis
    May 10, 2004
  4. Replies:
    19
    Views:
    282
    sisyphus
    Apr 7, 2008
  5. John
    Replies:
    1
    Views:
    120
    shimmyshack
    Mar 31, 2007
Loading...

Share This Page