develooper Front page | perl.perl5.porters | Postings from September 2000

[PATCH lib/overload.pm] Sanaty checking of arguments to overload::constant

Thread Next
From:
abigail
Date:
September 1, 2000 02:48
Subject:
[PATCH lib/overload.pm] Sanaty checking of arguments to overload::constant
Message ID:
20000901094654.6476.qmail@foad.org
This patch fixes the following problems by issueing relevant warnings,
and skipping non-sensible arguments to overload::constant.

    $ perl -Moverload -wle 'overload::constant qq  => sub {}'
    Use of uninitialized value in bitwise or (|) at
    /opt/perl/lib/5.6.0/overload.pm line 134.
    $ perl -Moverload -wle 'BEGIN {overload::constant q => []} $_ = "foo"'  
    Not a CODE reference at -e line 1.
    Propagated at -e line 1, near "= "foo""
    Execution of -e aborted due to compilation errors.
    $ perl -Moverload -wle 'BEGIN {overload::constant "q"} $_ = "foo"'
    constant(q): $^H{q} is not defined at -e line 1, near "= "foo""
    Execution of -e aborted due to compilation errors.
    $


Abigail


*** lib/overload.pm.orig	Fri Sep  1 04:49:45 2000
--- lib/overload.pm	Fri Sep  1 05:31:07 2000
***************
*** 127,137 ****
  	 dereferencing	  => '${} @{} %{} &{} *{}',
  	 special	  => 'nomethod fallback =');
  
  sub constant {
    # Arguments: what, sub
    while (@_) {
!     $^H{$_[0]} = $_[1];
!     $^H |= $constants{$_[0]} | $overload::hint_bits;
      shift, shift;
    }
  }
--- 127,162 ----
  	 dereferencing	  => '${} @{} %{} &{} *{}',
  	 special	  => 'nomethod fallback =');
  
+ use warnings::register;
  sub constant {
    # Arguments: what, sub
    while (@_) {
!     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;
    }
  }
***************
*** 1335,1340 ****
--- 1360,1386 ----
  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
  

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About