
yapp
User
Nov 1, 2001, 1:15 PM
Post #7 of 10
(4112 views)
|
Re: ? about CGI.PM input format in regards to SPLIT
[In reply to]
|
Can't Post
|
|
Whoa. I never realized it was that bad, but here are some other notes. Type use CGI::Carp qw(fatalsToBrowser); at the beginning of your program to see the errors in your browser. If you re-open STDERR to an other handle, your errors won't be dumped into the error log. You can choose your own log file, or reopen STDERR to STDOUT (the screen). However, if you dump something into STDOUT, a HTTP header should be printed first. The CGI::Carp module also has a method for that task, so you're not bothered with re opening STDERR. You can also reopen STDERR to a just-created file (which should be cleared up everytime your script starts) There are also the $SIG{'__DIE__'} and $SIG{'__WARN__'} variables in perl. They contain a subroutine reference (= \&subname). The subroutine is called every time a die or warn error is thrown. (this included the ones inside a eval()). However, why don't you visit http://www.perl.com and get a perl interpreter for yourself. Then you can test and debug your script locally first. If you succed in installing a webserver and link perl with cgi files, you can test everything locally. Then you don't need to worry about large server logs anymore. Second about the use strict. use strict 'vars' means that all the variables you use should be declared with my() first (BLOCK scoped). This prevents quite some typoos, which causes a new variable to be initialized with that name (and the old is then not used or updated). use strict 'refs' helps you with references. Then you can't write $$var and assuming $var is a reference. Then is MUST be a reference. use strict 'subs' tells the compiler all the subroutine and constants should be declared first. This is just as useful as use strict 'vars'. The -w makes perl more verbose (display more warnings). It also notifies you when your programming style causes perl to slow down, etc. -w is useful because a variable with an undef value can't be used anywhere. If that annoys you, you can set $^W to 0 (no -w) or $^W to 1 (using -w) at runtime. -w can be irritating, an undef value would print out an empty string when -w is not used. Here are some techniques to avoid errors with -w:
#!/usr/bin/perl -w use strict; my $X = 34; my($Y, $Z) = (45, 23); my $Text; # undef value print $Text if defined $Text; &SubroutineCall( $Text || '' ); # Replaces $Text with '' is $Text evaluates to false (undef, empty or zero) $Text ||= ''; # Replace with '' if the value is '', undef or 0. print qq[Text is $Text\n]; # is OK now. my $Text2; $Text2 = '' if not defined $Text2; # better because 0 is also false. $Text2 = '' unless defined $Text2; # other perl style. The ( ) characters aren't needed now. #..... # somewhat further in the script foreach ($Var1, $Text $Text2, $Var4, $X, $Y, $Z) { # $_ not references to one of the values (one by one). $_ = '' if not defined; # $_ can be omitted in defined() call s/a+/b/g; # substitutes all the aaaaaas into a b (global match) chomp; # remove \n at the end print; # prints $_ } One of the webservers my familysite is located doesn't even allow scripts without -w !!!!! A 500 Internal Server error is returned when -w is missing. -Tw would even be better and safer, but is even more difficult to handle.
|