The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

warnings::DynamicScope - Provides warning categories in dynamic scope.

SYNOPSIS

  require warnings::DynamicScope;

  package GrandMother;
  use warnings::register;
  
  sub deliver {
      my $self;
      $^W{GrandMother} && warn "You have warned by grandma.";
      bless \$self;
  }
  
  package Mother;
  use base "GrandMother";
  use warnings::register;
  
  sub deliver {
      $^W{Mother} && warn "You have warned by mom.";
      $_[0]->SUPER::deliver();
  }
  
  package main;
  
  $^W = 1;
  $^W{GrandMother} = 0;
  
  my $me = Mother->deliver(); # => You have warned by mom.
  

DESCRIPTION

This module provides warning categories in dynamic scope through the special variable "%^W".

VARIABLES

This modules brings new special variable called "%^W". Yes, it is very similar to special variable "$^W" in appearance, but these are different things.

But you can use it like special variable "$^W":

 require warnings::DynamicScope;

 package MyPkg;
 use warnings::register;
 
 sub my_func {
     if ($^W{MyPkg}) {
         print "Don't do it!!\n";
     } else {
         print "That's fine\n";
     }
 }
 
 package main;
 $^W = 1;

 {
     local $^W{MyPkg} = 0;
     MyPkg::my_func();
 }
 MyPkg::my_func();

This code prints:

 That's fine.
 Don't do it!!

That's all.

OBJECTIVE

The reason why I decided to write a new module which provides capability similar to warnings pragma is that I found the limitation of "warnings::enabled" and "warnings::warnif" function.

While I'm writing my module, I noticed that the code like below will not work as I intended:

  use warnings;
  
  package GrandMother;
  use warnings::register;
  
  sub deliver {
      my $self;
      warnings::warnif("GrandMother", "You have warned by grandma.");
      bless \$self;
  }
  
  package Mother;
  use base "GrandMother";
  use warnings::register;
  
  sub deliver {
      warnings::warnif("Mother", "You have warned by mom.");
      $_[0]->SUPER::deliver();
  }
  
  package main;
  no warnings "GrandMother";
  no warnings "Mother";
  
  my $me = Mother->deliver(); # => You have warned by grandma.

In this code, I intended to inhibit warning messages from each class "GrandMother" and "Mother".

But, if I run this code, warning in "GrandMother" class will be emitted. So that means the information by pragma 'no warnings "GrandMother"' would not be passed to "GrandMother" class properly.

I thought this comes from nature of these function that these functions uses warnings information in static scope. (They gets static scope information from stack of caller function.)

So, I started write this module to make warnings categories work with dynamic scope.

TIPS

If you don't like Perl's variable abbreviation like $^W, try:

 use English qw(WARNING);

BUGS/LIMITATION

Most of warning categories predefined in Perl must be set at compile time, or it will not work.

See, code below does not work:

 $^W{uninitialized} = 0;

So, you have to rewrite it as:

 BEGIN {
   $^W{uninitialized} = 0;
 }

This brings same result of:

 no warnings 'uninitialized';

and the effect of dynamic scope will be lost.

This is specification of Perl.

EXPORT

None by default.

SEE ALSO

perllexwarn

Perl Lexical Warnings.

Documentation about lexical warnings.

warnings

Perl pragma to control optional warnings.

You can use warning categories based on lexical scope, by using functions "warnings::enabled", etc.

warnings::register

warnings import function.

You can make your warning category with "warnings::register" pragma.

AUTHOR

Keitaro Miyazaki, <KHC03156@nifty.ne.jp>

COPYRIGHT AND LICENSE

Copyright (C) 2005 by Keitaro Miyazaki

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.6 or, at your option, any later version of Perl 5 you may have available.