This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Benchmark.t: add more diag output
[perl5.git] / lib / overload.pm
index 8954551..4a1912c 100644 (file)
@@ -1,6 +1,6 @@
 package overload;
 
-our $VERSION = '1.19';
+our $VERSION = '1.26';
 
 %ops = (
     with_assign         => "+ - * / % ** << >> x .",
@@ -8,8 +8,8 @@ our $VERSION = '1.19';
     num_comparison      => "< <= >  >= == !=",
     '3way_comparison'   => "<=> cmp",
     str_comparison      => "lt le gt ge eq ne",
-    binary              => '& &= | |= ^ ^=',
-    unary               => "neg ! ~",
+    binary              => '& &= | |= ^ ^= &. &.= |. |.= ^. ^.=',
+    unary               => "neg ! ~ ~.",
     mutators            => '++ --',
     func                => "atan2 cos sin exp abs log sqrt int",
     conversion          => 'bool "" 0+ qr',
@@ -30,11 +30,11 @@ sub nil {}
 sub OVERLOAD {
   $package = shift;
   my %arg = @_;
-  my ($sub, $fb);
-  *{$package . "::()"} = \&nil; # Make it findable via fetchmethod.
+  my $sub;
+  *{$package . "::(("} = \&nil; # Make it findable via fetchmethod.
   for (keys %arg) {
     if ($_ eq 'fallback') {
-      for my $sym (*{$package . "::(fallback"}) {
+      for my $sym (*{$package . "::()"}) {
        *$sym = \&nil; # Make it findable via fetchmethod.
        $$sym = $arg{$_};
       }
@@ -42,7 +42,7 @@ sub OVERLOAD {
       warnings::warnif("overload arg '$_' is invalid")
         unless $ops_seen{$_};
       $sub = $arg{$_};
-      if (not ref $sub and $sub !~ /::/) {
+      if (not ref $sub) {
        $ {$package . "::(" . $_} = $sub;
        $sub = \&nil;
       }
@@ -62,17 +62,18 @@ sub import {
 sub unimport {
   $package = (caller())[0];
   shift;
+  *{$package . "::(("} = \&nil;
   for (@_) {
       warnings::warnif("overload arg '$_' is invalid")
         unless $ops_seen{$_};
-      delete $ {$package . "::"}{"(" . $_};
+      delete $ {$package . "::"}{$_ eq 'fallback' ? '()' : "(" .$_};
   }
 }
 
 sub Overloaded {
   my $package = shift;
   $package = ref $package if ref $package;
-  mycan ($package, '()');
+  mycan ($package, '()') || mycan ($package, '((');
 }
 
 sub ov_method {
@@ -80,7 +81,7 @@ sub ov_method {
   return undef unless $globref;
   my $sub = \&{*$globref};
   no overloading;
-  return $sub if !ref $sub or $sub != \&nil;
+  return $sub if $sub != \&nil;
   return shift->can($ {*$globref});
 }
 
@@ -251,7 +252,9 @@ illustrates the calling conventions:
     # * may recurse once - see table below
 
 Three arguments are passed to all subroutines specified in the
-C<use overload> directive (with one exception - see L</nomethod>).
+C<use overload> directive (with exceptions - see below, particularly
+L</nomethod>).
+
 The first of these is the operand providing the overloaded
 operator implementation -
 in this case, the object whose C<minus()> method is being called.
@@ -309,6 +312,12 @@ An appropriate implementation of C<--> might look like
         # ...
     sub decr { --${$_[0]}; }
 
+If the experimental "bitwise" feature is enabled (see L<feature>), a fifth
+TRUE argument is passed to subroutines handling C<&>, C<|>, C<^> and C<~>.
+This indicates that the caller is expecting numeric behaviour.  The fourth
+argument will be C<undef>, as that position (C<$_[3]>) is reserved for use
+by L</nomethod>.
+
 =head3 Mathemagic, Mutators, and Copy Constructors
 
 The term 'mathemagic' describes the overloaded implementation
@@ -360,8 +369,8 @@ hash C<%overload::ops>:
  num_comparison          => '< <= > >= == !=',
  '3way_comparison'=> '<=> cmp',
  str_comparison          => 'lt le gt ge eq ne',
- binary                  => '& &= | |= ^ ^=',
- unary           => 'neg ! ~',
+ binary                  => '& &= | |= ^ ^= &. &.= |. |.= ^. ^.=',
+ unary           => 'neg ! ~ ~.',
  mutators        => '++ --',
  func            => 'atan2 cos sin exp abs log sqrt int',
  conversion      => 'bool "" 0+ qr',
@@ -374,6 +383,7 @@ hash C<%overload::ops>:
 Most of the overloadable operators map one-to-one to these keys.
 Exceptions, including additional overloadable operations not
 apparent from this hash, are included in the notes which follow.
+This list is subject to growth over time.
 
 A warning is issued if an attempt is made to register an operator not found
 above.
@@ -406,7 +416,7 @@ evaluating an expression.
 =item * I<Assignments>
 
     +=  -=  *=  /=  %=  **=  <<=  >>=  x=  .=
-    &=  |=  ^=
+    &=  |=  ^=  &.=  |.=  ^.=
 
 Simple assignment is not overloadable (the C<'='> key is used
 for the L<Copy Constructor>).
@@ -436,7 +446,7 @@ even if C<$a> is a scalar.
 =item * I<Non-mutators with a mutator variant>
 
      +  -  *  /  %  **  <<  >>  x  .
-     &  |  ^
+     &  |  ^  &.  |.  ^.
 
 As described L<above|"Calling Conventions and Magic Autogeneration">,
 Perl may call methods for operators like C<+> and C<&> in the course
@@ -495,9 +505,6 @@ If C<E<lt>E<gt>> is overloaded then the same implementation is used
 for both the I<read-filehandle> syntax C<E<lt>$varE<gt>> and
 I<globbing> syntax C<E<lt>${var}E<gt>>.
 
-B<BUGS> Even in list context, the iterator is currently called only
-once and with scalar context.
-
 =item * I<File tests>
 
 The key C<'-X'> is used to specify a subroutine to handle all the
@@ -652,9 +659,9 @@ to C<'-='> and C<'--'> above:
 And other assignment variations are analogous to
 C<'+='> and C<'-='> (and similar to C<'.='> and C<'x='> above):
 
-              operator ||  *= /= %= **= <<= >>= &= ^= |=
-    -------------------||--------------------------------
-    autogenerated from ||  *  /  %  **  <<  >>  &  ^  |
+              operator ||  *= /= %= **= <<= >>= &= ^= |= &.= ^.= |.=
+    -------------------||-------------------------------------------
+    autogenerated from ||  *  /  %  **  <<  >>  &  ^  |  &.  ^.  |.
 
 Note also that the copy constructor (key C<'='>) may be
 autogenerated, but only for objects based on scalars.
@@ -670,7 +677,7 @@ expects.  The minimal set is:
 
     + - * / % ** << >> x
     <=> cmp
-    & | ^ ~
+    & | ^ ~ &. |. ^. ~.
     atan2 cos sin exp log sqrt int
     "" 0+ bool
     ~~
@@ -688,7 +695,8 @@ The specified function will be passed four parameters.
 The first three arguments coincide with those that would have been
 passed to the corresponding method if it had been defined.
 The fourth argument is the C<use overload> key for that missing
-method.
+method.  If the experimental "bitwise" feature is enabled (see L<feature>),
+a fifth TRUE argument is passed to subroutines handling C<&>, C<|>, C<^> and C<~> to indicate that the caller is expecting numeric behaviour.
 
 For example, if C<$a> is an object blessed into a package declaring
 
@@ -1056,7 +1064,7 @@ the stashes, which are rarely used directly, thus should not slow down
 Perl.)
 
 If a package uses overload, it carries a special flag.  This flag is also
-set when new function are defined or @ISA is modified.  There will be a
+set when new functions are defined or @ISA is modified.  There will be a
 slight speed penalty on the very first operation thereafter that supports
 overloading, while the overload tables are updated.  If there is no
 overloading present, the flag is turned off.  Thus the only speed penalty
@@ -1233,7 +1241,7 @@ Put this in F<symbolic.pm> in your Perl library directory:
 
 This module is very unusual as overloaded modules go: it does not
 provide any usual overloaded operators, instead it provides an
-implementation for L<C<nomethod>>.  In this example the C<nomethod>
+implementation for L</C<nomethod>>.  In this example the C<nomethod>
 subroutine returns an object which encapsulates operations done over
 the objects: C<< symbolic->new(3) >> contains C<['n', 3]>, C<< 2 +
 symbolic->new(3) >> contains C<['+', 2, ['n', 3]]>.
@@ -1677,6 +1685,10 @@ coincides with the current one.
 
 Barewords are not covered by overloaded string constants.
 
+=item *
+
+The range operator C<..> cannot be overloaded.
+
 =back
 
 =cut