in reply to One out of three ain't bad
Your grep solution would be intuitive to many monks, but it becomes more maintainable when paired with well-named variables or subroutines. My (current, and PBP-influenced) preferences:
my $number_of_true_variables = grep { $_ } $x, $y, $z; if ( $number_of_true_variables == 1 ) { print "Exactly one variable set\n"; }
sub number_of_true_variables { return scalar grep { $_ } @_; } if ( number_of_true_variables( $x, $y, $z ) == 1 ) { print "Exactly one variable set\n"; }
sub exactly_one_is_set { my $number_of_true_variables = grep { $_ } @_; return ( $number_of_true_variables == 1 ); } if ( exactly_one_is_set( $x, $y, $z ) ) { print "Exactly one variable set\n"; }
|
|---|