Newbie question

P

pdpi

Hi all,

I've only just started learning perl a couple of days ago (trying to do
some basic CGI), and have stumbled upon a bit of a doubt. From RTFMing,
I figured that, to print each line of a text file as an HTML paragraph,
something like this works:

#!/usr/bin/perl

use CGI;

my $cgi = new CGI;
open(BODY,"../data/body.txt");
print $cgi->header("text/html");
print $cgi->start_html;
while(<BODY>){
print $cgi->p($_);
}
print $cgi->end_html;

close(BODY);


however, what I want next is to make the file's lines be rendered as
entries in an unordered list/rows in a table (i figure at that point
it's all mostly the same). The obvious way to do this is
print <ul>
while(<BODY>){
print $cgi->li($_);
}
print</ul>

but that feels somewhat lacking in elegance or consistency, especially
considering the existence of the ul function in CGI.pm, but I'm
stumped. It feels as though there ought to be a simple, elegant way to
do this. Any takers?
 
A

A. Sinan Unur

#!/usr/bin/perl

use strict;
use warnings;

use CGI();

because you are using the OO interface.
my $cgi = new CGI;
open(BODY,"../data/body.txt");

1. The current directory need not be the directory where your script is
located.

2. Always check the return value of open.

3. Prefer the 3-argument form of open, and lexical filehandles.

So:

open my $body, '<', '/some/path/data/body.txt'
or die "Cannot open /some/path/data/body.txt: $!";
print $cgi->header("text/html");
print $cgi->start_html;
while(<BODY>){
print $cgi->p($_);
}

If the file is small enough, you can read all the lines into an array,
and use that (see THE DISTRIBUTIVE PROPERTY OF HTML SHORTCUTS in perldoc
CGI):

my @lines = <$body>;
print $cgi->ul($cgi->li(\@lines));

Sinan
 
B

Brian McCauley

pdpi said:
Subject: Newbie question

Please put the subject of your post in the Subject of your post.
I've only just started learning perl a couple of days ago (trying to do
some basic CGI), and have stumbled upon a bit of a doubt. From RTFMing,
I figured that, to print each line of a text file as an HTML paragraph,
something like this works:

#!/usr/bin/perl

You should get into the habit of doing...

use strict;
use warnings;

The sooner you do the less painfull it will be.
use CGI;

my $cgi = new CGI;
open(BODY,"../data/body.txt");

Even in example code you should at least put minimal error handling on
open().

open(BODY,"../data/body.txt") or die $!;

Note: If you are just learning Perl now you may as well get into the
habit of using the newer style of open.

open(my $body, '<', "../data/body.txt") or die $!;

Note: Making assumptions about the current working directory in CGI is
generally a bad idea as it's not defined.
print $cgi->header("text/html");
print $cgi->start_html;
while(<BODY>){
print $cgi->p($_);
}
print $cgi->end_html;

close(BODY);


however, what I want next is to make the file's lines be rendered as
entries in an unordered list/rows in a table (i figure at that point
it's all mostly the same). The obvious way to do this is
print <ul>
while(<BODY>){
print $cgi->li($_);
}
print</ul>

but that feels somewhat lacking in elegance or consistency, especially
considering the existence of the ul function in CGI.pm, but I'm
stumped. It feels as though there ought to be a simple, elegant way to
do this. Any takers?

You can do

print $cgi->start_ul;

and

print $cgi->end_ul;
 

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,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top