Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Warnings are enabled, but they aren't?

by muthm (Acolyte)
on Feb 27, 2023 at 09:25 UTC ( [id://11150629]=perlquestion: print w/replies, xml ) Need Help??

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

Hello wise monks.

I was playing around with features and warnings because I would like to create my own Moose version that not only enables all warnings like Moose does (which is great), but also disables experimental::signatures warnings, because I don't want to repeat writing no warnings 'experimental::signatures'; over and over again.

But my question is only about the strange and unexpected results that I get when I use warnings::enabled() for checking whether enabling and disabling warnings from my import sub works.

In fact, for me it looks like warnings::enabled does not treat the enabled/disabled state of 'standard' warning categories like once or void correctly. I am sure there is a good explanation why the following code behaves as it does, and it would certainly help me if I understood why.

#!/usr/bin/env perl use feature 'say'; use feature 'signatures'; # Switch all warnings *off*, and only some warnings *on* explicitly: no warnings; use warnings qw( void once experimental::signatures ); # Let's check whether the warnings are generated: # Checking 'void' warnings: "hello"; # Results in a 'Useless use of a constant ("hello") in void context' # warning (as expected). # Checking 'once' warnings: $x = 1; # Results in a 'Name "main::x" used only once: possible typo' warning # (as expected). # Checking 'experimental::signatures' warnings: sub foo( $param ) { say "foo( $param )" } # Results in a 'The signatures feature is experimental' warning # (as expected; no warning in perl v5.36 since there, the feature is # not experimental anymore.) # Let's see what 'warnings::enabled' returns, # and let 'warnings::warnif' print an own warning for each enabled # category (and they should all be enabled!): for my $category ( qw( void once experimental::signatures ) ) { say "warnings::enabled( $category ) returns ", warnings::enabled( $category ) ? "TRUE" : "**FALSE**", "."; warnings::warnif( $category, "my own warning for $category" ); } # Prints this: # warnings::enabled( void ) returns **FALSE**. # warnings::enabled( once ) returns **FALSE**. # warnings::enabled( experimental::signatures ) returns TRUE. # my own warning for experimental::signatures at [...] # # But no output for the 'void' or 'once' categories. 1;

Thank you for a ration of your wisdom!

Replies are listed 'Best First'.
Re: Warnings are enabled, but they aren't?
by tobyink (Canon) on Feb 27, 2023 at 10:16 UTC

    warnings::enabled() doesn't check if warnings are enabled "here" because you should already know that. It checks whether warnings are enabled in your caller. (In particular, it walks up the call stack to find the first caller level which is in a different package.)

    use v5.16; package Foo { no warnings; sub foo { print( warnings::enabled( 'once' ) ? 'enabled' : 'disabled' ); } } package Bar { use warnings qw( once ); sub bar { Foo::foo(); } } Bar::bar();

    Basically, it's designed so you can check if your caller wants warnings, to help you decide whether to warn them about stuffs.

      Wow!
      That makes sense!
      Thank you for this help!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11150629]
Approved by hippo
Front-paged by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-03-28 10:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found