This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Finish up perl5134delta
[perl5.git] / warnings.pl
index 149d010..e57ed1f 100644 (file)
@@ -168,13 +168,13 @@ sub mkRange
 {
     my @a = @_ ;
     my @out = @a ;
-    my $i ;
 
-
-    for ($i = 1 ; $i < @a; ++ $i) {
+    for my $i (1 .. @a - 1) {
        $out[$i] = ".."
-          if $a[$i] == $a[$i - 1] + 1 && $a[$i] + 1 == $a[$i + 1] ;
+          if $a[$i] == $a[$i - 1] + 1
+             && ($i >= @a  - 1 || $a[$i] + 1 == $a[$i + 1] );
     }
+    $out[-1] = $a[-1] if $out[-1] eq "..";
 
     my $out = join(",",@out);
 
@@ -696,11 +696,25 @@ sub unimport
 
 my %builtin_type; @builtin_type{qw(SCALAR ARRAY HASH CODE REF GLOB LVALUE Regexp)} = ();
 
+sub MESSAGE () { 4 };
+sub FATAL () { 2 };
+sub NORMAL () { 1 };
+
 sub __chk
 {
     my $category ;
     my $offset ;
     my $isobj = 0 ;
+    my $wanted = shift;
+    my $has_message = $wanted & MESSAGE;
+
+    unless (@_ == 1 || @_ == ($has_message ? 2 : 0)) {
+       my $sub = (caller 1)[3];
+       my $syntax = $has_message ? "[category,] 'message'" : '[category]';
+       Croaker("Usage: $sub($syntax)");
+    }
+
+    my $message = pop if $has_message;
 
     if (@_) {
         # check the category supplied.
@@ -736,8 +750,29 @@ sub __chk
         $i = _error_loc(); # see where Carp will allocate the error
     }
 
-    my $callers_bitmask = (caller($i))[9] ;
-    return ($callers_bitmask, $offset, $i) ;
+    # Defaulting this to 0 reduces complexity in code paths below.
+    my $callers_bitmask = (caller($i))[9] || 0 ;
+
+    my @results;
+    foreach my $type (FATAL, NORMAL) {
+       next unless $wanted & $type;
+
+       push @results, (vec($callers_bitmask, $offset + $type - 1, 1) ||
+                       vec($callers_bitmask, $Offsets{'all'} + $type - 1, 1));
+    }
+
+    # &enabled and &fatal_enabled
+    return $results[0] unless $has_message;
+
+    # &warnif, and the category is neither enabled as warning nor as fatal
+    return if $wanted == (NORMAL | FATAL | MESSAGE)
+       && !($results[0] || $results[1]);
+
+    require Carp;
+    Carp::croak($message) if $results[0];
+    # will always get here for &warn. will only get here for &warnif if the
+    # category is enabled
+    Carp::carp($message);
 }
 
 sub _error_loc {
@@ -747,61 +782,26 @@ sub _error_loc {
 
 sub enabled
 {
-    Croaker("Usage: warnings::enabled([category])")
-       unless @_ == 1 || @_ == 0 ;
-
-    my ($callers_bitmask, $offset, $i) = __chk(@_) ;
-
-    return 0 unless defined $callers_bitmask ;
-    return vec($callers_bitmask, $offset, 1) ||
-           vec($callers_bitmask, $Offsets{'all'}, 1) ;
+    return __chk(NORMAL, @_);
 }
 
 sub fatal_enabled
 {
-    Croaker("Usage: warnings::fatal_enabled([category])")
-  unless @_ == 1 || @_ == 0 ;
-
-    my ($callers_bitmask, $offset, $i) = __chk(@_) ;
-
-    return 0 unless defined $callers_bitmask;
-    return vec($callers_bitmask, $offset + 1, 1) ||
-           vec($callers_bitmask, $Offsets{'all'} + 1, 1) ;
+    return __chk(FATAL, @_);
 }
 
 sub warn
 {
-    Croaker("Usage: warnings::warn([category,] 'message')")
-       unless @_ == 2 || @_ == 1 ;
-
-    my $message = pop ;
-    my ($callers_bitmask, $offset, $i) = __chk(@_) ;
-    require Carp;
-    Carp::croak($message)
-       if vec($callers_bitmask, $offset+1, 1) ||
-          vec($callers_bitmask, $Offsets{'all'}+1, 1) ;
-    Carp::carp($message) ;
+    return __chk(FATAL | MESSAGE, @_);
 }
 
 sub warnif
 {
-    Croaker("Usage: warnings::warnif([category,] 'message')")
-       unless @_ == 2 || @_ == 1 ;
-
-    my $message = pop ;
-    my ($callers_bitmask, $offset, $i) = __chk(@_) ;
-
-    return
-        unless defined $callers_bitmask &&
-               (vec($callers_bitmask, $offset, 1) ||
-               vec($callers_bitmask, $Offsets{'all'}, 1)) ;
-
-    require Carp;
-    Carp::croak($message)
-       if vec($callers_bitmask, $offset+1, 1) ||
-          vec($callers_bitmask, $Offsets{'all'}+1, 1) ;
-
-    Carp::carp($message) ;
+    return __chk(NORMAL | FATAL | MESSAGE, @_);
 }
 
+# These are not part of any public interface, so we can delete them to save
+# space.
+delete $warnings::{$_} foreach qw(NORMAL FATAL MESSAGE);
+
 1;