On Jun 28, Ela Jarecka said:
>if ( ( $reqrec->fillServ($ServRef) ) == undef ) {
> print "fillRec failed! \n";
> exit;
>}
This prints a message to STDOUT (or the currently select()ed handle), and
exits the program with an error code of 0.
Testing for equality with undef is a mistake.
>if ( ( $reqrec->fillServ($ServRef) ) == undef ) {
> die "fillRec failed! \n";
>}
This prints a message to STDERR (or gets caught by $SIG{__DIE__} first),
and exits the program with a NON-ZERO error code (signifying that the
program terminated abnormally).
Testing for equality with undef is (still) a mistake.
>$reqrec->fillServ($ServRef) or die "fillServ failed";
This is good.
>In fillServ function, I use 'return;' upon failure and 'return 1;' if
>everything is OK.
You should not test for equality with undef, because undef becomes NOT
undef when used in any context.
print 2 + undef; # becomes 2 + 0, albeit with a warning
print "hi" . undef; # becomes "hi" . "", albeit with a warning
if ($x == undef) {
# you get here if $x is 0, '', or undef
}
if ($x eq undef) {
# you get here if $x is '' or undef
}
Instead, use the defined() approach:
if (not defined $x) {
# $x can ONLY have the value of undef if it gets here
}
However, you can treat undef as false (since it is) in a boolean
expression:
# this IS a silly way to write this function, but nevermind
sub large_number {
return if $_[0] < 1_000_000;
return 1;
}
if (large_number($foo)) {
# it's big if it got here
}
You don't need to test for == 1 or == undef or defined(...), because 1 is
true, and undef is false.
--
Jeff "japhy" Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/
I am Marillion, the wielder of Ringril, known as Hesinaur, the Winter-Sun.
Are you a Monk? http://www.perlmonks.com/ http://forums.perlguru.com/
Perl Programmer at RiskMetrics Group, Inc. http://www.riskmetrics.com/
Acacia Fraternity, Rensselaer Chapter. Brother #734
** Manning Publications, Co, is publishing my Perl Regex book **