In article <k1g6rk$ct1$>,
Tim McDaniel <> wrote:
>In article <>,
>Henry Law <> wrote:
>>I'm checking parameters to a little utility I'm writing. It uses
>>Getopt::Std, which returns the parameters in a hash %opts.
>>
>>The logic of the utility requires both flags -h and -p to be specified
>>or neither. I'm coding the part that checks whether that is true.
>>
>>I'm sure there's a neater way than this (which does work, admittedly):
>>
>>die "Flags -h and -p must be specified together\n"
>> if (exists $opts{p} && !exists $opts{h}) || (exists $opts{h} &&
>>!exists $opts{p});
>
> if exists $opts{p} == exists $opts{h}
To expand on that,
IF you have Perl boolean values, by which I mean there are only two
possible values, where one evaluates to true and one evaluates to
false (and I believe that exists fulfils that),
THEN != is the exclusive-or function: it is true if an only if exactly
one of its operands is true.
And therefore == is the inverse: true if and only if both operands are
true or both operands are false.
You may not have such a boolean. For example, you might have a
function returning a number, and you want to just check that both are
zero or both are non-zero. You can't do
somefunc($x) == somefunc($y)
because maybe that's (for example)
12 == 27
which is incorrect.
So !! is Perl's "convert to boolean" operator:
!!(something that evaluates to false) -> ''
!!(something that evaluates to true) -> 1
So the more general way to do exclusive or for values that are simply
either Perl true or Perl false, but might be any true or false value,
!!val1 != !!val2
and so the general "both or neither" is
!!val1 == !!val2
--
Tim McDaniel,