Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Control warnings for each module by creating new warning categories

by bliako (Abbot)
on Jun 24, 2024 at 11:57 UTC ( [id://11160187]=perlquestion: print w/replies, xml ) Need Help??

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

For a recent post (Solve IO::Pty warning issues from Net::OpenSSH) I tried to create a new category for warnings for a module and then disable it but it failed (perl 5.34.0) when I used it as *I expected*:

{ package XXX; use strict; # register a new warnings category with this package name use warnings::register; sub expected { warn "ajajaj"; } sub correct { if( warnings::enabled() ){ warn "ajajaj"; } else { print "not warning!\n" } } 1; } # it's already loaded... #use XXX; no warnings('XXX'); # this says 'no' (as expected) print "enabled: ".(warnings::enabled('XXX')?"yes":"no")."\n"; XXX::expected(); # this still warns XXX::correct(); # this does not warn

Firstly, correct me if I am doing something wrong.

Secondly, am I expecting too much to just register a category within a module and then anyone from outside it, to enable or disable it? Why the check to warnings::enabled() in each and every warn() statement of that module. That makes refactoring to using warning categories very difficult.

Replies are listed 'Best First'.
Re: Control warnings for each module by creating new warning categories
by pryrt (Abbot) on Jun 24, 2024 at 13:28 UTC
    change instances of warn to warnings::warnif, like
    sub expected { warnings::warnif "ajajaj"; }

    When I make that change in your example, I get the output:

    enabled: no not warning!

    To make this change globally, you'd just need to refactor via s/(?!warnings::)\bwarn\b/warnings::warnif/g

Re: Control warnings for each module by creating new warning categories
by LanX (Saint) on Jun 24, 2024 at 13:09 UTC

    I didn't try your code, but from looking into https://perldoc.perl.org/warnings#Reporting-Warnings-from-a-Module I think your fears are justified.

    (Tho the code examples might need correction regarding the package name abc:: )

    But ...

    > That makes refactoring to using warning categories very difficult

    IMHO... You could locally (i.e. package wide) replace warn for your XXX:: only and handle all the magic inside a single routine.

    Use subs for overriding built-ins.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-07-18 04:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

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