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 a435f89..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,6 +696,7 @@ 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 };
 
@@ -705,6 +706,15 @@ sub __chk
     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.
@@ -744,13 +754,25 @@ sub __chk
     my $callers_bitmask = (caller($i))[9] || 0 ;
 
     my @results;
-    foreach my $type (NORMAL, FATAL) {
+    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));
     }
-    return $wanted == (NORMAL | FATAL) ? @results : $results[0];
+
+    # &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 {
@@ -760,48 +782,26 @@ sub _error_loc {
 
 sub enabled
 {
-    Croaker("Usage: warnings::enabled([category])")
-       unless @_ == 1 || @_ == 0 ;
-
     return __chk(NORMAL, @_);
 }
 
 sub fatal_enabled
 {
-    Croaker("Usage: warnings::fatal_enabled([category])")
-  unless @_ == 1 || @_ == 0 ;
-
     return __chk(FATAL, @_);
 }
 
 sub warn
 {
-    Croaker("Usage: warnings::warn([category,] 'message')")
-       unless @_ == 2 || @_ == 1 ;
-
-    my $message = pop ;
-    require Carp;
-    Carp::croak($message) if __chk(FATAL, @_);
-    Carp::carp($message) ;
+    return __chk(FATAL | MESSAGE, @_);
 }
 
 sub warnif
 {
-    Croaker("Usage: warnings::warnif([category,] 'message')")
-       unless @_ == 2 || @_ == 1 ;
-
-    my $message = pop ;
-    my ($warn, $fatal) = __chk(NORMAL | FATAL, @_);
-
-    return unless $warn or $fatal;
-
-    require Carp;
-    Carp::croak($message) if $fatal;
-    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);
+delete $warnings::{$_} foreach qw(NORMAL FATAL MESSAGE);
 
 1;