J
Jay Buffington
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 &~.
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.
Are there any legitimate uses for &~? I can't think of one. I can't
find &~ documented anywhere. I've looked in perlop and the Camel
Book.
Why does this exist?
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.
Jay
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 &~.
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.
Are there any legitimate uses for &~? I can't think of one. I can't
find &~ documented anywhere. I've looked in perlop and the Camel
Book.
Why does this exist?
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.
Jay