Thanks Jim. I used
exit(0) unless $varA;
and it worked good from command line. When I try the script through a
browser pointing to the file like so
http://domain.com/cgi-bin/script.pl?varA=10
the script executes as it should.
But when I try to send 0 as the value (?varA=0), I get a code 500
internal error. Is this the normal response for a script to prematurely
exit using a browser to run it?
Jim Gibson wrote:
On 2/8/10 Mon Feb 8, 2010 4:56 PM, "Chris Coggins" <cacogg...@cox.net>
scribbled:
What is the proper syntax for a comparison at the end of a subroutine to
stop the script if a certain variable is "0" or ''?
sub routine{
(get data and process it)
if $varA = '' || $varA = "0" {
(stop script, print error message to user)
}
else{
(continue with script)
}
The scalar values '' and 0 (not in quotes) are 2 of the 3 logically false
scalar values. (The undef value is the third, and an empty array is
another.) If you can accept stopping your program if $varA is undef as well
as '' or 0, then you can simply test the logical value of $varA:
if( $varA ) {
# continue with program
}else{
exit(0); # stop program
}
You can use 'unless' instead of 'if' and swap blocks:
unless( $varA ) {
exit(0);
}else{
# continue with program
}
Since exit does not return, you can shorten this:
unless( $varA ) {
exit(0);
}
Or shorten again:
exit(0) unless $varA;
You can use print() to print a message before terminating. You can also use
die(), which will print its argument and terminate the program:
die("The variable \$varA does not have a valid value") unless $varA;
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/