The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Exception::Warning - Convert simple warn into real exception object

SYNOPSIS

  # Convert warn into exception and throw it immediately
  use Exception::Warning '%SIG' => 'die';
  eval { warn "Boom!"; };
  print ref $@;        # "Exception::Warning"
  print $@->warning;   # "Boom!"

  # Convert warn into exception without die
  use Exception::Warning '%SIG' => 'warn', verbosity => 4;
  warn "Boom!";   # dumps full stack trace

  # Can be used in local scope only
  use Exception::Warning;
  {
      local $SIG{__WARN__} = \&Exception::Warning::__WARN__;
      warn "Boom!";   # warn via exception
  }
  warn "Boom!";       # standard warn

  # Run Perl with verbose warnings
  $ perl -MException::Warning=%SIG,warn,verbosity=>3 script.pl

  # Run Perl which dies on first warning
  $ perl -MException::Warning=%SIG,die,verbosity=>3 script.pl

  # Run Perl which ignores any warnings
  $ perl -MException::Warning=%SIG,warn,verbosity=>0 script.pl

  # Debugging with increased verbosity
  $ perl -MException::Warning=:debug script.pl

DESCRIPTION

This class extends standard Exception::Base and converts warning into real exception object. The warning message is stored in warning attribute.

INHERITANCE

CONSTANTS

ATTRS : HashRef

Declaration of class attributes as reference to hash.

See Exception::Base for details.

ATTRIBUTES

This class provides new attributes. See Exception::Base for other descriptions.

warning : Str {ro}

Contains the message which is set by $SIG{__WARN__} hook.

message : Str = "Unknown warning"

Contains the message of the exception. This class overrides the default value from Exception::Base class.

string_attributes : ArrayRef[Str] = ["message", "warning"]

Meta-attribute contains the format of string representation of exception object. This class overrides the default value from Exception::Base class.

default_attribute : Str = "warning"

Meta-attribute contains the name of the default attribute. This class overrides the default value from Exception::Base class.

IMPORTS

use Exception::Warning '%SIG';
use Exception::Warning '%SIG' => 'warn';

Changes $SIG{__WARN__} hook to Exception::Warning::__WARN__.

use Exception::Warning '%SIG' => 'die';

Changes $SIG{__WARN__} hook to Exception::Warning::__DIE__ function.

use Exception::Warning ':debug';

Changes $SIG{__WARN__} hook to Exception::Warning::__WARN__ and sets verbosity level to 4 (maximum).

no Exception::Warning '%SIG';

Undefines $SIG{__DIE__} hook.

PERFORMANCE

The Exception::Warning module can change $SIG{__WARN__} hook. It costs a speed for simple warn operation. It was tested against unhooked warn.

  -------------------------------------------------------
  | Module                              |         run/s |
  -------------------------------------------------------
  | undef $SIG{__WARN__}                |      276243/s |
  -------------------------------------------------------
  | $SIG{__WARN__} = sub { }            |      188215/s |
  -------------------------------------------------------
  | Exception::Warning '%SIG'           |        1997/s |
  -------------------------------------------------------
  | Exception::Warning '%SIG', verb.=>0 |       26934/s |
  -------------------------------------------------------

It means that Exception::Warning is significally slower than simple warn. It is usually used only for debugging purposes, so it shouldn't be an important problem.

SEE ALSO

Exception::Base.

BUGS

If you find the bug or want to implement new features, please report it at http://rt.cpan.org/NoAuth/Bugs.html?Dist=Exception-Warning

AUTHOR

Piotr Roszatycki <dexter@cpan.org>

LICENSE

Copyright (C) 2008, 2009 by Piotr Roszatycki <dexter@cpan.org>.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

See http://www.perl.com/perl/misc/Artistic.html