From: Abigail Date: Fri, 1 Sep 2000 05:46:54 +0000 (-0400) Subject: Sanaty checking of arguments to overload::constant X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=6b82e2f59e3c46c3c35c70f18f42e8c715a2cb5e;p=p5sagit%2Fp5-mst-13.2.git Sanaty checking of arguments to overload::constant Message-ID: <20000901094654.6476.qmail@foad.org> p4raw-id: //depot/perl@6969 --- diff --git a/lib/overload.pm b/lib/overload.pm index f7772c1..cce5df7 100644 --- a/lib/overload.pm +++ b/lib/overload.pm @@ -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 above as code references can be + # blessed, and C 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 function of module C). +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