my %builtin_type; @builtin_type{qw(SCALAR ARRAY HASH CODE REF GLOB LVALUE Regexp)} = ();
+sub MESSAGE () { 4 };
sub FATAL () { 2 };
sub NORMAL () { 1 };
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.
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 {
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:
my %builtin_type; @builtin_type{qw(SCALAR ARRAY HASH CODE REF GLOB LVALUE Regexp)} = ();
+sub MESSAGE () { 4 };
sub FATAL () { 2 };
sub NORMAL () { 1 };
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.
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 {
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;