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

use of -w and warnings

by RolandGunslinger (Curate)
on Dec 10, 2003 at 16:10 UTC ( [id://313754] : perlquestion . print w/replies, xml ) Need Help??

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

I've seen some scripts on perlmonks that use both the #!perl -w as well as use warnings; My understand of use warnings is that when you do not specify any parameters that it functions essentially the same as #!perl -w. Is this redundant or is there a purpose here I'm not understanding? Thanks. Long days and pleasant nights...

Replies are listed 'Best First'.
Re: use of -w and warnings
by broquaint (Abbot) on Dec 10, 2003 at 16:13 UTC
    For a nice explanation of use warnings vs. -w and why the former is now prefered over the latter see perllexwarn. The gist of it being that lexically scoped warnings and optional warnings are a good thing™.


Re: use of -w and warnings
by Ovid (Cardinal) on Dec 10, 2003 at 16:16 UTC

    See perldoc for both warnings and perllexwarn. Basically, the warnings pragma gives you lexically scoped warnings and much finer grained control over the warnings. For example, if you don't want "redefined" warnings, but you don't want to suppress all of the other warnings, rather than create a customer $SIG{__WARN__} handler, you can specify no warnings 'redefine'.


    New address of my CGI Course.

Re: use of -w and warnings
by davido (Cardinal) on Dec 10, 2003 at 16:31 UTC
    The section from perllexwarn that you might find most interesting comes under the headline, "What's wrong with -w and $^W":

    Although very useful, the big problem with using -w on the command line to enable warnings is that it is all or nothing. Take the typical scenario when you are writing a Perl program. Parts of the code you will write yourself, but it's very likely that you will make use of pre-written Perl modules. If you use the -w flag in this case, you end up enabling warnings in pieces of code that you haven't written.

    The section then goes on to describe how $^W falls short of providing lexical scoping for -w.

    For simple code snippets, -w and use warnings are synonymous. But for more elaborate code, and scripts that use modules, or scripts that need lexical control over warnings, the pragma is preferable over the command-line switch.


Re: use of -w and warnings
by hardburn (Abbot) on Dec 10, 2003 at 16:16 UTC

    Occasionally, a programer who knows what they're doing will create code which causes a non-fatal error to be printed that they know is OK. In such a case, use warnings will allow you to put a no warnings for the area of code in question, but keep the warnings for the rest of the program. OTOH, using -w would require you to shut off warnings for the entire code.

    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    : () { :|:& };:

    Note: All code is untested, unless otherwise stated

Re: use of -w and warnings
by dvergin (Monsignor) on Dec 11, 2003 at 06:54 UTC
    There are four responses (as I write this) from monks of stature, but no one has responded to the original question: Is there any point to using both '-w' and 'use warnings' in the same script -- or is that a mistake.

    My take is, it's an error. If this is wrong, someone will likely chime in.

    "Perl is a mess and that's good because the
    problem space is also a mess.
    " - Larry Wall