[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: ot perl help request



On Mon, Apr 22, 2002 at 11:02:54AM -0700, justin cunningham wrote:
> Hello, I'm trying to get this script to do error checking but it prints
> the error page every time regardless if there is or isn't input data.
> It works ok without the if statement but I wanted it to do the
> errorchecking.  I spent a good amount of time trying to figure this out
> on my own before emailing the list but I've learned perl is pretty
> unforgiving when you have a misplace character or two.  
[...]
> #!/usr/bin/perl 

You'll find Perl a much more helpful language if you get into the habit
of using the -w flag, and preferably 'use strict'. For CGI scripts, I'd
recommend using the -T flag as well, which taints untrusted input to
help you write more secure code. It also helps to run CGI scripts at the
command line before subjecting them to a web server.

If you use strict, all variables have to be either fully-qualified
(using :: to indicate the package) or declared with 'my', 'our' (Perl
5.6 and above), or 'use vars'.

> my $cgi = new CGI;
> $cgi->import_data('Q');

There's no such method. Did you mean import_names?

> open (DATA, ">> $DATAFILE");

Be sure to check the return value of open() in case it couldn't write to
$DATAFILE. Something like this is the usual idiom:

  open (DATA, ">> $DATAFILE") or die "Couldn't open $DATAFILE: $!";

> print DATA qq|$Q::name, $Q::email, $Q::menu, $ENV{REMOTE_ADDR}\n|;

I'd agree with Elizabeth that you should use $cgi->param() instead. With
this construction, you have to jump through some hoops to avoid
"possible typo" warnings with -w.

> if  ($Q::{"name"} ne "") {

'$Q::{"name"}' isn't what you think at all - you want '$Q::name'. Of
course, if you use param() and assign the result to a temporary variable
then you'll avoid the whole issue.

> print $cgi->redirect(-location=>$ERR= '/error.html');
[...]
> print $cgi->redirect(-location=>$OUTPAGE= '/thanks.html');

I'm not sure why you have the = and the stuff following it. Drop it. You
don't really need -location either, so just do $cgi->redirect($ERR) etc.

Perl isn't all that unforgiving. Actually, it's one of the more
forgiving languages I can think of. You just need to be used to the
basic syntax, which is generally quite natural but occasionally a little
bizarre.

Cheers,

-- 
Colin Watson                                  [cjwatson@flatline.org.uk]


-- 
To UNSUBSCRIBE, email to debian-user-request@lists.debian.org 
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org



Reply to: