Richard Heintze <[EMAIL PROTECTED]> wrote:
 
: What is happening to my second argument here?
: It is not being passed!
: 
: use CGI qw(:standard);
: $q = new CGI;

    I know this is only a test, but
"qw(:standard)" imports a whole lot of subs into
your script that are not needed with the object-
oriented style you're using.


: sub print_args {
:   my ($a, $b, $c) = @_;
:   print "a = $a b = $b c=$c\n";
: }
: &print_args("abc", $q->param('next'), "xyz");

    In this call, $q->param('next') is called in
list context. When 'next' is not passed and
$q->param('next') is called in list context an
empty array '()' is returned.

    In this instance the above is equivalent to:

print_args("abc", (), "xyz");

Or:

print_args("abc", "xyz");


    One reason for returning () instead of
( undef ) is for testing. If I expect a list
from the field I only need to test the length
of the return.

my @list = $q->param('list');
report_error('list') unless @list;


: # However, if make a slight change, it works!
: my $n = $q->param("next");
: &print_args("abc", $n, "xyz");

    In this call, $q->param('next') is called in
scalar context.  When 'next' is not passed and
$q->param('next') is called in scalar context
undef is returned.

    In this instance the above is equivalent to:

print_args("abc", undef, "xyz");


: What is going on here? That was nasty!

    This trap is easily fixed by using strict
and warnings. If this script had been written
as below it would report an 'uninitialized'
error. Forcing the programmer to validate form
fields in some way.


#!/usr/bin/perl -T

use strict;
use warnings;

use CGI;
my $q = CGI->new();

print_args( 'abc', $q->param('next'), 'xyz');

sub print_args {
    my( $a, $b, $c ) = @_;
    print "a = $a b = $b c=$c\n";
}

__END__

    Here are few methods to validate cgi
fields.

# Assuming DEFAULT_NEXT is a constant
my $next = $q->param('next') || DEFAULT_NEXT;

Or:

# Assuming DEFAULT_NEXT is a constant
$q->param('next') || $q->param( next => DEFAULT_NEXT );

Or:

# Assuming report_error() is a defined error
#   handler that doesn't return
report_error('next') unless $q->param('next');


HTH,

Charles K. Clarkson
-- 
Head Bottle Washer,
Clarkson Energy Homes, Inc.
Mobile Home Specialists
254 968-8328







-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to