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

Dear Master Monks,

Is there any difference between explicitly declaring the warnings pragma or using the -w switch?

Did it change between perl versions?

Thanks.

Walking the road to enlightenment... I found a penguin and a camel on the way.....
Fancy a yourname@perl.me.uk? Just ask!!!

Replies are listed 'Best First'.
Re: #!/usr/bin/perl -w and use warnings;
by dave_the_m (Monsignor) on Apr 23, 2005 at 13:48 UTC
    -w turns on warning for the whole Perl program, including any included modules etc; 'use warnings' enables them only in the current lexical scope.

    Dave.

Re: #!/usr/bin/perl -w and use warnings;
by japhy (Canon) on Apr 23, 2005 at 13:48 UTC
    The -w has a global effect on your entire program. The warnings pragma was introduced in Perl 5.6 and allows for scoped sections of warning-enabled code, with the ability to turn on and off specific classes of warning messages.
    _____________________________________________________
    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
Re: #!/usr/bin/perl -w and use warnings;
by tlm (Prior) on Apr 23, 2005 at 13:49 UTC

    This is discussed in warnings and perllexwarn; from the former:

    The "warnings" pragma is a replacement for the command line flag "-w", but the pragma is limited to the enclosing block, while the flag is global.

    the lowliest monk

      Yes, I never thought of that, in that it would be useful to only have warnings for some sections, but the "-w" would be good to check other people modules.

      Do I understand that correctly?

      Walking the road to enlightenment... I found a penguin and a camel on the way.....
      Fancy a yourname@perl.me.uk? Just ask!!!

        The -w flag won't help you if the module's author explicitly turned warnings off. (I've run into this problem when a module's author turns off all warnings in a block, instead of selectively turning off a particular warning.) E.g.

        % perl -wle 'use warnings; { no warnings; $x == 1 } print "ok"' ok % perl -Wle 'use warnings; { no warnings; $x == 1 } print "ok"' Useless use of numeric eq (==) in void context at -e line 1. Name "main::x" used only once: possible typo at -e line 1. Use of uninitialized value in numeric eq (==) at -e line 1. ok

        Update: Clarified the original reply.

        the lowliest monk

Re: #!/usr/bin/perl -w and use warnings;
by ambrus (Abbot) on Apr 23, 2005 at 14:01 UTC

    You've already got the correct answer. Just one more comment: the -w flag is equivalent to BEGIN { $^W = 1; }

Re: #!/usr/bin/perl -w and use warnings;
by hubb0r (Pilgrim) on Apr 23, 2005 at 14:09 UTC
    Plus, with the advent of the warnings pragma, you can use warnings for your entire program, but then locally scope a
    no warnings; or no warnings qw(uninitialized);
    for a block of code where there is a good reason not to use warnings.
Re: #!/usr/bin/perl -w and use warnings;
by brian_d_foy (Abbot) on Apr 23, 2005 at 17:06 UTC

    Besides the other answers, use warnings implicitly requires perl 5.6 or later. People who have to support earlier versions of perl don't use the pragma.

    --
    brian d foy <brian@stonehenge.com>

      Have far back can you use the -w option?

      Just going to check the history files on perldoc.

      Walking the road to enlightenment... I found a penguin and a camel on the way.....
      Fancy a yourname@perl.me.uk? Just ask!!!

        Perl4 had the -w switch, and back then everything fit in one manpage (man perl) so that's where you need to look.

        You can still download perl4.036 from CPAN and just look for yourself. You don't have to actually install it :)

        --
        brian d foy <brian@stonehenge.com>

      Was it introduced with Perl 5?

      I can't find any info on Perl 4, which was before my time.

      Walking the road to enlightenment... I found a penguin and a camel on the way.....
      Fancy a yourname@perl.me.uk? Just ask!!!

        The -w switch was in Perl4.

        Even though it was before your time, Google still remembers it. :)

        --
        brian d foy <brian@stonehenge.com>
Re: #!/usr/bin/perl -w and use warnings;
by gaal (Parson) on Apr 23, 2005 at 23:03 UTC