diff options
author | Nicholas Clark <nick@ccl4.org> | 2010-06-20 18:00:31 +0100 |
---|---|---|
committer | Nicholas Clark <nick@ccl4.org> | 2010-06-23 08:44:42 +0100 |
commit | 96183d25c8bf116d31888a79fd63a86056518da1 (patch) | |
tree | d8cd504205e771e7843586409137be286cddc57c /lib/warnings.pm | |
parent | 8787a7475e25239a0fffec22fe068e97240c2a51 (diff) | |
download | perl-96183d25c8bf116d31888a79fd63a86056518da1.tar.gz |
Move parameter validation, and carp/croak generation into warnings::__chk.
This results in enabled, fatal_enabled, warnif, and warn as wrappers to __chk
selecting their desired behaviour, and eliminates duplicated code.
Diffstat (limited to 'lib/warnings.pm')
-rw-r--r-- | lib/warnings.pm | 54 |
1 files changed, 27 insertions, 27 deletions
diff --git a/lib/warnings.pm b/lib/warnings.pm index c86ea68f7b..eedbc3285b 100644 --- a/lib/warnings.pm +++ b/lib/warnings.pm @@ -411,6 +411,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 }; @@ -420,6 +421,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. @@ -459,13 +469,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 { @@ -475,49 +497,27 @@ 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; # ex: set ro: |