From 6dd857431f51e510fbff1fc7f983b04624551018 Mon Sep 17 00:00:00 2001 From: Adriano Ferreira Date: Thu, 23 Nov 2006 15:43:12 -0200 Subject: [PATCH] [perl #36689] &=, |= and ^= overloads are nbot documented From: "Adriano Rodrigues" Message-ID: <73ddeb6c0611231143y1e3461dbqf30f5fce16698b5c@mail.gmail.com> p4raw-id: //depot/perl@29373 --- lib/overload.pm | 10 +++++++--- lib/overload.t | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/lib/overload.pm b/lib/overload.pm index 690be22..1ca22b4 100644 --- a/lib/overload.pm +++ b/lib/overload.pm @@ -117,7 +117,7 @@ sub mycan { # Real can would leave stubs. 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", @@ -350,13 +350,17 @@ arrays, C is used to compare values subject to C. =item * I - "&", "^", "|", "neg", "!", "~", + "&", "&=", "^", "^=", "|", "|=", "neg", "!", "~", C stands for unary minus. If the method for C 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, 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 "++", "--", @@ -437,7 +441,7 @@ A computer-readable form of the above table is available in the hash 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', diff --git a/lib/overload.t b/lib/overload.t index 6dafa07..ade87f2 100644 --- a/lib/overload.t +++ b/lib/overload.t @@ -47,7 +47,7 @@ sub numify { 0 + "${$_[0]}" } # Not needed, additional overhead package main; $| = 1; -use Test::More tests => 509; +use Test::More tests => 512; $a = new Oscalar "087"; @@ -1253,3 +1253,36 @@ foreach my $op (qw(<=> == != < <= > >=)) { 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"); +} -- 1.8.3.1