It's unusual to save the value of a comparison, but it is very common to use a variable in an if statement. Consider this code:
# get office_id for this office
my $office_id = get_office_id();
if ($office_id) {
# found an office
} else {
# no office found
}
The hypothetical get_office_id() function could be written in such a way that the office id returned will either be a positive integer or undef if the office id cannot be found. Then the $office_id can be used directly in an if to test to result.
Of course, this will be wrong if 0 is a vaild office id. Then you need to do:
# get office_id for this office
my $office_id = get_office_id();
if (defined $office_id) {
# found an office
} else {
# no office found
}
Although this is a common pattern it is actually not the best way to write functions in Perl. A better way is to separate the status code from the return value:
# get office_id for this office
my ($ok, $office_id) = get_office_id();
if ($ok) {
# found an office
} else {
# no office found
}
That way there can be no confusion between the office id itself and the success of the call.
-sam
|