diff options
author | Abigail <abigail@abigail.be> | 2000-09-01 01:46:54 -0400 |
---|---|---|
committer | Jarkko Hietaniemi <jhi@iki.fi> | 2000-09-01 13:52:27 +0000 |
commit | 6b82e2f59e3c46c3c35c70f18f42e8c715a2cb5e (patch) | |
tree | d3e20928d13e84ee88616586d139147764f837d0 /lib/overload.pm | |
parent | 745e1e411ae11e5f34aad2fe998c476bd939bc0d (diff) | |
download | perl-6b82e2f59e3c46c3c35c70f18f42e8c715a2cb5e.tar.gz |
Sanaty checking of arguments to overload::constant
Message-ID: <20000901094654.6476.qmail@foad.org>
p4raw-id: //depot/perl@6969
Diffstat (limited to 'lib/overload.pm')
-rw-r--r-- | lib/overload.pm | 50 |
1 files changed, 48 insertions, 2 deletions
diff --git a/lib/overload.pm b/lib/overload.pm index f7772c1564..cce5df7852 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<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 |