rrwo has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to use the warnings pragma for my modules. In a simple module:

  package foo;

  use warnings::register;

  sub bar {
    if (warnings::enabled) {
      warnings::warn "this is a warning";
    }
  }

The above works properly if I say 'use warnings' or 'no warnings' in my calling code.

But in larger (object oriented) modules it does not work. Or rather, warnings::enabled always returns false. If in the module I enable warnings, it works but I cannot turn off the warnings in my calling code.

Is there a bug in warnings, or is there some overlooked and possibly poorly-documented feature of warnings that I'm missing out on?

Also: Is there a way to get Carp working properly with warnings? (From what I've read, not yet. See http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2000-11/msg01297.html)

  • Comment on How do I get warnings to work properly?

Replies are listed 'Best First'.
Re (tilly) 1: How do I get warnings to work properly?
by tilly (Archbishop) on Dec 07, 2000 at 19:17 UTC
    On your last question, wanna help? :-)

    The current status of the world is this. I rewrote the internals of Carp, the rewritten version is in the latest snapshot of Perl. I need to do some cleanup and documentation work, then write a test suite. (I am sick today, perhaps I will get some of that done.) It will be a while before I get around to studying the entire warnings system. The mechanism in my rewrite should (he blithely assumes) be enough to support the current warning semantics fairly easily and remove the OO related bugs. That may take some work.

    OTOH I have a life with responsibilities, and I am not immune to distractions. If someone more motivated got to looking at how warnings should work I would be the loudest cheerer. (I am not looking forward to that.) Frankly my main interest is just seeing Carp get cleaned up a little and then fixing problems I saw in Exporter.

    Carp
    by rrwo (Friar) on Dec 09, 2000 at 02:57 UTC
      I'll have to take look. I'm the cretin responsible for Win32::EventLog::Carp (which uses some Carp internals to carp into the Windows NT event log).
        When I posted what I posted above I had not yet finished building the new version. It looks like my addition was held temporarily based on the strict stuff. (And possibly the current lack of tests.)

        If you follow p5p you will see the changes again soon.

(tye)Re: How do I get warnings to work properly?
by tye (Sage) on Dec 07, 2000 at 22:57 UTC

    I believe that the use warnings pragma is meant to be package scoped so if you want your caller to affect the warning levels in your package, then you need to provide routines in your package that change your package's warning levels and have the caller use those.

            - tye (but my friends call me "Tye")

      I did a bit of hacking with warnings.pm. What's throwing the module off is its use of the caller function.

      Here is an illustration:

      package MyAbc;
      
      warnings::register;
      
      sub foo
      {
        warnings::warn "foo", if (warnings::enabled);
        bar;
      }
      
      sub bar
      {
        warnings::warn "bar", if (warnings::enabled);
      }
      

      Warnings will work in foo because it is "top-level", but they do not work in bar because it's a function within a function.

      That is, they don't work if I use warnings 'MyAbc', but they will work if I run Perl with the -w switch.

      Correction ... if I run foo from a calling module, we don't see the warning from foo but we do see it from bar. However, when we run bar directly we don't see it.

      Even stranger, a module that I'm working on has the opposite effect of my correction. This is strange indeed.

      Any idea how to do this? From the warnings documentation it looks like one just says:

         use warnings 'MyPackage';
      

      or

         no warnings 'MyPackage';
      

      Assuming one has called use warnings::regsiter() from within MyPackage. Is there some other way to do this? I've even tried putting the use warnings::register in the BEGIN or INIT blocks to see if that would work.