printing html with perl

G

Guy

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
 
N

Nathan Keel

Guy said:
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.
 
X

Xho Jingleheimerschmidt

Guy said:
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
 
S

Scott Bryce

Guy said:
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
 
T

Tad J McClellan

Keith Keller said:
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 :)

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


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

ccc31807

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
 
A

A. Sinan Unur

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 <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 
B

Ben Bullock

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;
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top