Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Printing with CGI.pm

by cal (Beadle)
on Jul 20, 2002 at 19:43 UTC ( [id://183652]=perlquestion: print w/replies, xml ) Need Help??

cal has asked for the wisdom of the Perl Monks concerning the following question:

Hello,
I am having trouble understanding print staements in CGI.pm
The while loop keeps printing "Content-Type: text/html"
I understand that the default content for print header()is "Content-Type: text/html"
But normally the content type is at the top of the page. Does any one know how to do that?
I can't seem to find any good info on using print with CGI.pm
#!/usr/local/bin/perl -w use strict; use diagnostics; use CGI qw(:standard); ############################# #initialize variable ############################# my $db ="events.txt"; open (FILE, "<" . $db) || die "Well, whats up.. cant open file"; #flock (FILE, 2 ); my $lc = 0; ###################################### # Start of while loop ###################################### while (<FILE>) { my $line = $_; $line =~ s/\n$//; my @events = split ("::", $line); if ($events[1] eq ""){$lc = $line}; ############################## # Print Parish Events ############################## print header(), start_html(-title=>'Script'), "<HR> <table border='0' width='100%'> <tr> <td width='100%'> <table + border='0' width='100%'> + <tr> + <td width='100%'> + <table border='0' width='100%'> + <tr> + <td width='22%'><font SIZE='+1'color='gray' +><B>$events[0] $events[1], $events[2]</B></font></td> + <td width='78%'><font SIZE='+1'color='black +'><B>$events[3]</B></font></td> + </tr> + </table> + </td> + </tr> + <tr> + <td width='100%'><font SIZE='2'>$events[4]</font></td> + </tr> </tabl +e> </td> </tr> </table>", end_html(); ##################################### if ($events[1] eq ""){ #flock (FILE, 8 ); close (FILE); } }

Replies are listed 'Best First'.
Re: Printing with CGI.pm
by metadatum (Scribe) on Jul 20, 2002 at 19:52 UTC
    Well, print header() prints the Content type.... I would move it outside your loop...
      Thanks,
      When I move the print header(); outside of the loop I loose all the HTML formatting. I just get the data from the text file. Is there a particular way to do this?
      Cal
        In your original code, you have one long print statement. When you moved the print header() outside of the loop, did you forget to leave a print statement inside the loop? You may want to move the start_html() out of the loop, as well. Some other thoughts:
        • As your code is right now, it looks like you'll get each line in a separate table. Is that What You Want?
        • Your code might look better if you were to use CGI.pm's table functions (table, td, Tr, etc.), which you already have imported by using :standard. I refer to Lincoln Stein's Official Guide to Programming wth CGI.pm. I'm sure there are online resources for this; I've just never looked.
        HTH

        --

        There are 10 kinds of people -- those that understand binary, and those that don't.

Re: Printing with CGI.pm
by Kanji (Parson) on Jul 20, 2002 at 20:02 UTC
    But normally the content type is at the top of the page. Does any one know how to do that?

    Place your print header before anything else, and make sure it isn't in a loop unless you have some way of surpressing multiple calls...

    print header; while ( <FILE> ) { # ... }
    while ( <FILE> ) { # ... print header unless $already_printed_header++; # ... }

    However, in more recent versions of CGI, the functionality of the latter is actually built in for you, but needs to be enabled by supplying the -unique_headers argument to your use CGI...

    use CGI qw/ -unique_headers /; while ( <FILE> ) { # ... print header; # ... }

        --k.


(jptxs) Re: Printing with CGI.pm
by jptxs (Curate) on Jul 21, 2002 at 13:45 UTC
    You may want to take a gander at Ovid's CGI Tutorial. It will explain all the advice here and more you may want to know.

    We speak the way we breathe. --Fugazi

Re: Printing with CGI.pm
by Jazz (Curate) on Jul 22, 2002 at 06:09 UTC

    Lincoln has a CGI.pm manual online. But if you can, grab a copy of Official Guide to Programming wth CGI.pm. It's invaluable as a desktop reference.

    As mephit noted, you're already importing all of the standard CGI.pm functions by using use CGI qw(:standard). Since you may as well use what's already imported, below is a sample of what your program can look like with using CGI.pm.

    #!/usr/bin/perl -w use strict; use CGI qw/ :standard *table /; use CGI::Pretty; # not necessary, but it makes for more legible html o +utput my $db ="test.txt"; # moved header and start_html outside of loop # print takes a list, so you can join several items to print with comm +as print header, start_html( -title => 'Script' ), hr, start_table( { -width => '100%', -border => '0' } ); # It's more helpful to include $! in the error open( FILE, "<$db" ) or die "Well, whats up.. can't open file $!\n"; while ( <FILE> ) { # no need to define $line here -- $_ is already defined, so you ca +n use it # directly chomp; # Just a preference note. When working with arrays, I prefer to u +se # something a little easier to maintain, like: # my ( $name, $date, $something, $somethingelse ) = split/::/; # it's a bit easier than trying to remember the order of the field +s in $db my @events = split/::/; print Tr( td( { -width => '22%' }, font( { -size => '+1', color => 'gray' }, b( "$events[0] $events[1], $events[2]" ) ) ), td( { -width => '78%' }, font( { -size => '+1', color => 'black' }, $events[3] ) ) ), Tr( td( { -colspan => '2'}, font( { -size => '2' }, $events[4] ) ) ); } print end_table, end_html; close FILE or die "Can't close $!\n";
Re: Printing with CGI.pm
by Wally Hartshorn (Hermit) on Jul 22, 2002 at 22:05 UTC

    Before you spend too much time hard-coding HTML in your program, I would strongly urge you to use the HTML::Template module. You would still use CGI.pm, of course, since that would handle all of the CGI-specific stuff, but virtually all of your HTML would go into external template files, which you (or a non-Perl web person) could easily modify with having to munge up your Perl code.

    A simple template file might be:

    <html> <head> <title><!-- TMPL_VAR NAME='TITLE' --></title> </head> <body> <h1><!-- TMPL_VAR NAME='HEADING' --></h1> <ul> <TMPL_LOOP NAME='LISTOFSTUFF'> <li><TMPL_VAR NAME='SOMETEXT'></li> </TMPL_LOOP> </ul> </body> </html>

    You would then just have your Perl program supply the values of the variables. (The HTML::Template docs would supply the details. It's really very easy.) Separating your Perl code from your HTML code will save you LOTS of grief in the future.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://183652]
Approved by Courage
Front-paged by jptxs
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2024-04-24 08:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found