jaybuffington@gmail.com (Jay Buffington) wrote in message news:<e45b6a57.0409091426.7796c616@posting.google. com>...[color=blue]
> I was looking through some very badly written code at work today and I
> came across this line:
> my $hold_status=$invoice->{'HOLD_STATUS'} &~ HS_MAIL_IN_PHOTO;
>
> HS_MAIL_IN_PHOTO is defined as a constant equal to 2.
>
> I think the original author meant == instead of &~.[/color]
No, it is not uncommon to use bitwise operations for flags. So this
mean that
$invoice->{HOLD_STATUS} is a number being used as a bit array and we
wist to copy it into $hold_status but without the flag
HS_MAIL_IN_PHOTO.
[color=blue]
> I tried this:
> #!/usr/bin/perl -w
>
> use strict;
>
> my $num1 = 0x110;
> my $num2 = 0x111;
> my $num3 = 0x011;
>
> print "num1 &~ num2\n" if $num1 &~ $num2;
> print "num2 &~ num3\n" if $num2 &~ $num3;
> print "num1 &~ num3\n" if $num1 &~ $num3;
>
> and got this output:
> num1 &~ num2
>
> Which has convinced me that this is a bitwise pattern matching
> operator.[/color]
Yes, it returns a number whose binary representation has 1s where
there were 1s in the LHS but not in RHS.
Used in a boolean context it tells you if there are any bits set in
the LHS that weren't in the RHS.
Of course all this assumes the LHS is a number. The bitwise operators
in Perl are about the only thing that will treat $num1='666' and
$num1=666 differently (for details RTFM).
[color=blue]
> Are there any legitimate uses for &~? I can't think of one.[/color]
Yes there are many. I must admit to not having used in in Perl but
I've used it often in C (and indeed in SQL).
[color=blue]
> I can't find &~ documented anywhere. I've looked in perlop and the Camel
> Book.
>
> Why does this exist?[/color]
Because geiven that there's no token &~ in Perl then perl will
interpret it as two tokens & and ~ (which are both documented in the
aforementioned places).
[color=blue]
> As a side note, I also played with &&~, which works like this:
> print "foo" if (&sub1 &&~ &sub2); # execute sub1 and sub2 and only
> print foo if sub1 returns true.[/color]
Well actually if sub2 returns ~0 then it won't print foo.
This newsgroup does not exist (see FAQ). Please do not start threads
here.