Sanaty checking of arguments to overload::constant
authorAbigail <abigail@abigail.be>
Fri, 1 Sep 2000 05:46:54 +0000 (01:46 -0400)
committerJarkko Hietaniemi <jhi@iki.fi>
Fri, 1 Sep 2000 13:52:27 +0000 (13:52 +0000)
Message-ID: <20000901094654.6476.qmail@foad.org>

p4raw-id: //depot/perl@6969

lib/overload.pm

index f7772c1..cce5df7 100644 (file)
@@ -127,11 +127,36 @@ sub mycan {                               # Real can would leave stubs.
         dereferencing    => '${} @{} %{} &{} *{}',
         special          => 'nomethod fallback =');
 
+use warnings::register;
 sub constant {
   # Arguments: what, sub
   while (@_) {
-    $^H{$_[0]} = $_[1];
-    $^H |= $constants{$_[0]} | $overload::hint_bits;
+    if (@_ == 1) {
+        if (warnings::enabled) {
+            require Carp;
+            Carp::carp ("Odd number of arguments for overload::constant");
+        }
+        last;
+    }
+    elsif (!exists $constants {$_ [0]}) {
+        if (warnings::enabled) {
+            require Carp;
+            Carp::carp ("`$_[0]' is not an overloadable type");
+        }
+    }
+    elsif (!ref $_ [1] || "$_[1]" !~ /CODE\(0x[\da-f]+\)$/) {
+        # Can't use C<ref $_[1] eq "CODE"> above as code references can be
+        # blessed, and C<ref> would return the package the ref is blessed into.
+        if (warnings::enabled) {
+            require Carp;
+            $_ [1] = "undef" unless defined $_ [1];
+            Carp::carp ("`$_[1]' is not a code reference");
+        }
+    }
+    else {
+        $^H{$_[0]} = $_[1];
+        $^H |= $constants{$_[0]} | $overload::hint_bits;
+    }
     shift, shift;
   }
 }
@@ -1336,6 +1361,27 @@ key (in fact a presence of this method shows that this package has
 overloading enabled, and it is what is used by the C<Overloaded>
 function of module C<overload>).
 
+The module might issues the following warnings:
+
+=over 4
+
+=item Odd number of arguments for overload::constant
+
+(W) The call to overload::constant contained an odd number of arguments.
+The arguments should come in pairs.
+
+=item `%s' is not an overloadable type
+
+(W) You tried to overload a constant type the overload package is unaware of.
+
+=item `%s' is not a code reference
+
+(W) The second (fourth, sixth, ...) argument of overload::constant needs
+to be a code reference. Either an anonymous subroutine, or a reference
+to a subroutine.
+
+=back
+
 =head1 BUGS
 
 Because it is used for overloading, the per-package hash %OVERLOAD now