Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Selectively importing warnings

by coolmichael (Deacon)
on Nov 15, 2013 at 22:34 UTC ( #1062810=perlquestion: print w/ replies, xml ) Need Help??
coolmichael has asked for the wisdom of the Perl Monks concerning the following question:

Following in Ovid's footsteps, I'm trying to remove boilerplate from my code. I'm generally happy with the result, but for warnings.

I have a module with an import method. It's only purpose is to enable strict and warnings and a few other pragmas. But, I don't want it to enable all warnings, and this is where I'm a bit stumped.

package Foo; # lots of use statements here. use Import::Into; sub import { my $target = caller; 'strict'->import::into($target); 'warnings'->import::into($target, qw(FATAL utf8)); if ($] >= 5.017011) { # THIS IS THE PROBLEM LINE 'warnings'->unimport::out_of($target, 'experimental::smartmatc +h'); } }

The problem is 'warnings'->unimport::out_of($target, 'experimental::smartmatch'); turns off all warnings, not just the one I want.

unimport::out_of() is calling warnings->unimport('experimental::smartmatch'); - which I think is the same as calling no warnings 'experimental::smartmatch';. But it isn't working.

So how do enable warnings (except one) via an import sub?

update: This is Perl 5.18.1. I also fixed some formatting.

Comment on Selectively importing warnings
Select or Download Code
Re: Selectively importing warnings (works)
by Anonymous Monk on Nov 16, 2013 at 00:32 UTC

    I think it works :)

    $ perl import-into-warnings-notToolkit.pl f is not init but no warning Argument "A" isn't numeric in addition (+) at import-into-warnings-not +Toolkit.pl line 19. f is not a number and you're warned 0

    So whatever the deal is with experimental::smartmatch it might be a bug :)

    Here are all warnings forced for comparison

    $ perl -W import-into-warnings-notToolkit.pl Use of uninitialized value $f in concatenation (.) or string at import +-into-warnings-notToolkit.pl line 17. f is not init but no warning Argument "A" isn't numeric in addition (+) at import-into-warnings-not +Toolkit.pl line 19. f is not a number and you're warned 0

    the code

    #!/usr/bin/perl -- BEGIN { package Fake; $INC{'Fake.pm'}=__FILE__; use Import::Into; sub import { my $target = caller; 'strict'->import::into($target); 'warnings'->import::into($target); 'warnings'->unimport::out_of($target, 'uninitialized'); } } package Snake; use Fake; my $f ; print "f is not init but no warning $f \n"; $f = 'A'; print "f is not a number @{[ 0 + $f ]}\n"; __END__

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (13)
As of 2014-10-30 17:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (208 votes), past polls