num_comparison => "< <= > >= == !=",
'3way_comparison'=> "<=> cmp",
str_comparison => "lt le gt ge eq ne",
- binary => "& | ^",
+ binary => '& &= | |= ^ ^=',
unary => "neg ! ~",
mutators => '++ --',
func => "atan2 cos sin exp abs log sqrt int",
=item * I<Bit operations>
- "&", "^", "|", "neg", "!", "~",
+ "&", "&=", "^", "^=", "|", "|=", "neg", "!", "~",
C<neg> stands for unary minus. If the method for C<neg> is not
specified, it can be autogenerated using the method for
subtraction. If the method for C<!> is not specified, it can be
autogenerated using the methods for C<bool>, or C<"">, or C<0+>.
+The same remarks in L<"Arithmetic operations"> about
+assignment-variants and autogeneration apply for
+bit operations C<"&">, C<"^">, and C<"|"> as well.
+
=item * I<Increment and decrement>
"++", "--",
num_comparison => '< <= > >= == !=',
'3way_comparison'=> '<=> cmp',
str_comparison => 'lt le gt ge eq ne',
- binary => '& | ^',
+ binary => '& &= | |= ^ ^=',
unary => 'neg ! ~',
mutators => '++ --',
func => 'atan2 cos sin exp abs log sqrt',
package main;
$| = 1;
-use Test::More tests => 509;
+use Test::More tests => 512;
$a = new Oscalar "087";
undef $obj;
is ($ref, undef);
}
+
+{
+ package bit;
+ # bit operations have overloadable assignment variants too
+
+ sub new { bless \$_[1], $_[0] }
+
+ use overload
+ "&=" => sub { bit->new($_[0]->val . ' & ' . $_[1]->val) },
+ "^=" => sub { bit->new($_[0]->val . ' ^ ' . $_[1]->val) },
+ "|" => sub { bit->new($_[0]->val . ' | ' . $_[1]->val) }, # |= by fallback
+ ;
+
+ sub val { ${$_[0]} }
+
+ package main;
+
+ my $a = bit->new(my $va = 'a');
+ my $b = bit->new(my $vb = 'b');
+
+ $a &= $b;
+ is($a->val, 'a & b', "overloaded &= works");
+
+ my $c = bit->new(my $vc = 'c');
+
+ $b ^= $c;
+ is($b->val, 'b ^ c', "overloaded ^= works");
+
+ my $d = bit->new(my $vd = 'd');
+
+ $c |= $d;
+ is($c->val, 'c | d', "overloaded |= (by fallback) works");
+}