my %form = (var1 => $q->param('var1'),
var2 => $q->param('var2'),
var3 => $q->param('var3'));
to:
my %form = $q->Vars();
for a spot of maintainer efficiency :)
/J\
| [reply] [d/l] [select] |
I'm puzzled that you say, it works, as I get errors:
Global symbol "@reqform" requires explicit package name at test017.pl
+line 16.
Global symbol "@reqform" requires explicit package name at test017.pl
+line 21.
Global symbol "@errormsg" requires explicit package name at test017.pl
+ line 22.
Global symbol "@errormsg" requires explicit package name at test017.pl
+ line 22.
Global symbol "@errormsg" requires explicit package name at test017.pl
+ line 24.
Global symbol "@errormsg" requires explicit package name at test017.pl
+ line 34.
Bareword "var1" not allowed while "strict subs" in use at test017.pl l
+ine 16.
Bareword "var2" not allowed while "strict subs" in use at test017.pl l
+ine 16.
Bareword "var3" not allowed while "strict subs" in use at test017.pl l
+ine 16.
test017.pl had compilation errors.
change
my @reqform = qw(var1 var2 var3); # quote them!
and add
my @errormsg;
alex pleiner <alex@zeitform.de>
zeitform Internet Dienste | [reply] [d/l] [select] |
I think you should call your subs as:
PrintError() and CARRAY_ON_PROCESSING_FORM()
As the &sub way to call a sub passes @_ as arguments and disables prototype checking, as described in perlsub.
Also, I would pass @errormsg to PrintError as arguments.
PrintError(@errormsg) And then in PrintError:
foreach(@_){}
| [reply] [d/l] [select] |
i think this should be a comprehensive solution to your problem. this folds in suggestions by the other monks, and adds accuracy (which is important).
first off, i've turned on warnings and diagnostics, and disabled output buffering (this last one may be cargo-cult programming, but i always do it by default). i've used the object-oriented style of CGI programming, as it's my preference, and it's faster as per the documentation.
the meat of the error checking algorithm is this:
-
error if number of passed parameters does not match number of required parameters (this assumes the all required parameters, and only required parameters, should be passed -- your logic may vary)
scalar( @reqform )
!=
scalar( $q->param() )
-
error if passed parameter names do not match required parameter names
join( $; , sort( @reqform ) )
ne
join( $; , sort( $q->param() ) )
-
error if required parameters are missing values
while( ( $key, $value) = each %form )
{
push @missing_params, $key if $value eq '';
}
if( scalar @missing_params ) # ...
here's the full code. i must say, i've done all this work, but i haven't tested it. feel free to come down on me if it doesn't work, i deserve it.
#!/usr/bin/perl -w
use strict;
use diagnostics;
$|++;
use CGI;
my $q = new CGI;
my @reqform = qw( var1, var2, var3 );
print $q->header();
if(
( # different number of elements
scalar( @reqform )
!=
scalar( $q->param() )
)
||
( # element names do not match
join( $; , sort( @reqform ) )
ne
join( $; , sort( $q->param() ) )
)
)
{ # print error message for mismatched parameters
print
$q->start_html(),
$q->p( "ERROR: mismatched parameters:" ),
$q->p( "required: ", join ' ', sort @reqform ),
$q->p( "passed: ", join ' ', sort $q->param() ),
$q->end_html();
}
# get variables from submitted form
my %form = $q->Vars(); # note: this does not work with isindex tags
my ( $key, $value, @missing_params );
while( ( $key, $value) = each %form )
{
push @missing_params, $key if $value eq '';
}
if( scalar @missing_params )
{ print error message
print
$q->start_html(),
$q->p( "ERROR: undefined parameter(s): @missing_params" ),
$q->end_html();
}
# carry on...
~Particle ;Þ
riding the unicode bandwagon... | [reply] [d/l] [select] |
Thanks everyone!!
Im going to take all these tips and stick them together.
Im still learning but getting there :)
ThAtH0M
| [reply] |
One minor detail - since you are useing CGI.pm, you might as well replace print "Content-type: text/html\n\n"; with the cross-platform, OO, more correct and stylish print $q->header(); *grin*. Even though, as was pointed out to me in this node, it usually doesn't matter, it is the right thing to do. :)
You have moved into a dark place.
It is pitch black. You are likely to be eaten by a grue. | [reply] [d/l] [select] |