Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Trouble temporarily disabling warnings (was: Warnings)

by vek (Prior)
on Feb 02, 2002 at 14:10 UTC ( #142912=perlquestion: print w/replies, xml ) Need Help??
vek has asked for the wisdom of the Perl Monks concerning the following question:

Esteemed monks. I'm sure I'm missing something very simple here...

Just had to modify some code written by a co-worker. Usually I use -w to turn-on warnings in my code. The co-worker had opted for use warnings; instead.

A warning is generated from a tiny subroutine that I needed to temporarily disable. Now usually in my code I would do local $^W = 0; et voila - no warnings. A quick perusal of the doc led me to believe that I could say no warnings; and that would also temporarily turn off warnings. Didn't work.

Did I miss something from RTFM? Should no warnings; just before the offending code temporarily disable warnings a-la local $^W = 0;?

2002-03-31 Edit by the node bunny : Changed title from "Warnings"

Replies are listed 'Best First'.
Re: Warnings
by japhy (Canon) on Feb 02, 2002 at 14:19 UTC
    That depends if the error is raised at compile-time or run-time. If it's a compile-time error, no warnings will be required, or else some fanciful finagling of BEGIN { $^W = 0 } and BEGIN { $^W = 1 }.

    Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re: Warnings
by clintp (Curate) on Feb 02, 2002 at 16:15 UTC
    Another difference is that $^W=0 is temporal (or dynamic) and use warnings is lexical (scope limited).

    Setting $^W=0 causes the remainder of the subroutine and any subroutines called thereafter to not throw warnings at runtime. The warnings stay off until you restore $^W.

    use warnings works like other lexically scoped pragmas: it only affects things within the block it appears. If you step outside of that block (say, during a subroutine call) the warnings go back to the state they were in before entering the block. When you return to the block, they're back in effect.

Re: Warnings
by BazB (Priest) on Feb 02, 2002 at 15:34 UTC

    japhy has answered the meat of the question, but I'm just going to chip in with a note about your colleague's use warnings; over -w on the shebang line.

    use warnings; was introduced with Perl-5.6.0, and can be used in place of the -w switch in #!/usr/bin/perl -w

    If you are running - or need to ensure compatibilty with - a version of Perl earlier than 5.6.0, then you should use -w, otherwise it is preferable to use warnings.
    See perllexwarn and warnings for more info.



      Yeh, I knew about use warnings; only being compatible with versions >= 5.6.0 and to be honest I've never used it myself - I usually opt for -w. We're running 5.6.1 on all our Solaris boxes here so the compatability issue shouldn't be a factor. On second thoughts, I think I'll just remove the use warnings; and stick with -w, just in case...

      Thanks all.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://142912]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2018-01-20 03:51 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (226 votes). Check out past polls.