Perl, like C, doesn't have a boolean type. The language defines certain values as being false, and the rest are true. See How do I use boolean variables in Perl?, Truth and Falsehood.
In scalar context, grep
returns a count of matches. 0 is false and all other numbers are true, so treating the result of grep
as a boolean checks if there were any matches. So yes, grep
can very well be used as the argument of and
.
Let's do some cleanup.
You have the following:
scalar( grep { $_ eq $ServerTypeId } keys %ServerTypes ) > 0
and
grep { isRegionOK( $_, $DC ) == 1 } keys %$destinationSummary
Checking if a non-negative number is greater than zero is the same as checking if it's true or false.
scalar( grep { $_ eq $ServerTypeId } keys %ServerTypes )
and
grep { isRegionOK( $_, $DC ) == 1 } keys %$destinationSummary )
and
(and >
before that) evaluates its arguments in scalar context, so no need for scalar
.
grep { $_ eq $ServerTypeId } keys %ServerTypes
and
grep { isRegionOK( $_, $DC ) == 1 } keys %$destinationSummary
isRegionOK
region surely returns a boolean value.
grep { $_ eq $ServerTypeId } keys %ServerTypes
and
grep { isRegionOK( $_, $DC ) } keys %$destinationSummary
exists
is far more efficient at checking for the existence of a hash element by key.
exists($ServerTypes{$ServerTypeId})
and
grep { isRegionOK( $_, $DC ) } keys %$destinationSummary