RE: CGI.PM and HTML Templates

From: Charles K. Clarkson (cclarkson_at_htcomp.net)
Date: 06/18/04


To: "'John Pretti'" <john@web-connected.com>, <beginners@perl.org>
Date: Fri, 18 Jun 2004 09:51:03 -0500

John Pretti <john@web-connected.com> wrote:

: Can someone point me in the right direction of converting
: the following script to use an html template?

    You don't need html templates for this. Your pages are
static. They don't have any information added to them by
the cgi program. You can remove the html code from the
script though.

 
: #!/usr/bin/perl -w
: # perlUpload.cgi by John Pretti
: # Comments/Questions john[at]web-connected.com
: # Last modified 05/03/04
:
: # Load needed Perl modules
: use strict;
: use diagnostics;
: use CGI; # Make HTML easy to deal with
: use CGI::Carp 'fatalsToBrowser'; # Report errors to browser
: $CGI::POST_MAX=1024 * 100; # max 100K posts
:
: # Allowed file types
: my @good_extensions = ('doc', 'DOC', 'Doc', 'rtf', 'RTF', 'Rtf',
: 'pdf', 'PDF', 'Pdf');

    A hash would be more convenient.

# Allowed file types
my %is_valid_extension = (
    doc => 1,
    rtf => 1,
    pdf => 1,
);

    Then replace all this stuff:

    my $ingood_extensions = 0;
    foreach ( @good_extensions ) {
            if ( $_ eq $extension ) {
                $ingood_extensions = 1;
            }
    }
    if ($ingood_extensions == 1) {

    With:

    if ( %is_valid_extension( $extension ) ) {
 

: # Create new CGI object
: my $q = new CGI;
:
: if ( $q->param() ) {
:
: # Read filehandle from param and set to binary mode
: my $filehandle = $q->param('file');
: binmode($filehandle);
:
: # Strip off Windoze crap compliments of JHARRISS
: $_=$filehandle; s/.*\\//;
: my $filename=$_;
:
: # Check to see if filetype is allowed compliments of JHARRISS
: $_=$filename; s/.*\.//;
: my $extension=$_;
: my $ingood_extensions=0;
: foreach (@good_extensions) {
: if ($_ eq $extension) {
: $ingood_extensions=1;
: }
: }
: if ($ingood_extensions == 1) {
:
:
: # Open file for output
: open(OUT,">/www/web/htdocs/merlin/upload/$filename")
::: die $!;
: binmode(OUT);
:
:: 92
: $q->start_multipart_form,
: $q->filefield('file'),
: $q->br,
: $q->submit('Upload'),
: $q->end_form,
: $q->end_html;
: exit (1);
: }

    This section doesn't make sense. I doubt it does what
you want. It needs to be re-written. There's no print in
there. Why open a file and not do anything with it? And
what is the :92 label for?

: } else {

    Here are the first complete pages. Write the page as
a separate html file and redirect() to it here.

    if ($q->cgi_error()) {
        print $q->redirect( 'failure_form.html' );

    } else {
        print $q->redirect( 'file_upload.html')
    }

: if ($q->cgi_error()) {
: print $q->header,
: $q->start_html('Merlin
: Control Center'),
: $q->p("File upload has
: failed. Files must be
: under 4MB in size. Please try again"),
: $q->start_multipart_form,
: $q->filefield('file'),
: $q->br,
: $q->submit('Upload'),
: $q->end_form,
: $q->end_html;
: } else {
: print $q->header,
: $q->start_html('Merlin
: Control Center'),
:
: $q->img({-src=>'/merlin/images/usda.gif'}),
: $q->br,
: $q->start_multipart_form,
: $q->filefield('file'),
: $q->br,
: $q->submit('Upload'),
: $q->end_form,
: $q->end_html;
: }
:
: }
:
: While the above script, works perfectly, I would like to
: make it look a lot more attractive and I am having some
: troubles understanding how to get my html code to play
: nicely with CGI.pm. Thanks in advance.

    Are you sure the script works right. Let's take a
look. Here's the logic of the script.

my $q = new CGI;

if ( $q->param() ) {
    print 'we are in the file upload section';
    
} else {
    if ($q->cgi_error()) {
        print 'there is an error';

    } else {
        print 'printing form';
    }

}

    Now let's test that logic. First we'll test for the
error.

my $q = new CGI;

$q->param( cgi_error => 'Error' );

if ( $q->param() ) {

    .
    .
    .

Result:

we are in the file upload section

    Uh Oh! That's not working perfectly.

    Test your scripts better!

HTH,

Charles K. Clarkson

-- 
Mobile Homes Specialist
254 968-8328


Relevant Pages

  • Re: How to pass info to a CGI program without using a form?
    ... The image on the html page is specified by this html code: ... simply doing something like <url of CGI script>?key and check for the value ... and your script will find what key is there: ...
    (perl.beginners)
  • Re: any pointers please? combine words script
    ... use CGI qw/:standard/; ... # script is in development, ... # the html in another place ... sub get_html { ...
    (comp.lang.perl.misc)
  • Re: Need help
    ... My script works well with the sample data that I ... when the cgi script is run. ... If you are having a problem with your Perl script and want help, ... click in it and paste my html content or script in it. ...
    (comp.lang.perl.misc)
  • Re: CGI - HTTP 500 Internal server error
    ... What is a "HTML error"? ... most common problem is, as stated above, a missing script header. ... number and attach a STRSRVJOB and STRDBG to it to do debugging. ... To make CGI scripts a bit more verbose, have a look at the ScriptLog (or ...
    (comp.sys.ibm.as400.misc)
  • CGI.PM and HTML Templates
    ... script to use an html template? ... # perlUpload.cgi by John Pretti ... # Create new CGI object ... While the above script, works perfectly, I would like to make it look a lot ...
    (perl.beginners)