Forum Moderators: coopster & phranque

Message Too Old, No Replies

use warnings or -w

what is the difference?

         

PHP_Chimp

4:13 pm on Jan 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I am interested in the difference between the use warnings pragma and the -w switch. Iv looked and I cant find anyone actually giving any reason for there choice in using either version. However seeing as in my first post to this forum I was told -
The -w flag on the shebang line turns on warnings. But you should not use it and use the warnings pragma instead:
use warnings;

I am interested to know if there is actually a reason behind this, or if it is just a matter of personal preference/coding style.
They seem to me to be doing the same thing. They both turn on warnings.
The use warnings pragma can be turned off within blocks of code by using -

sub foo {
no warnings;
...
}

However it seems that as the -w switch sets the value $^W = 1, that you could use -

sub foo {
my $^W = 0;
...
}

to disable warnings within a subroutine. Ok, so my $^W=0 is not as readable as no warnings...surely this cant be the only point against -w?
As -w is a quicker to type and they appear to do the same thing, is there any reason for 1 version over the other?

perl_diver

11:00 pm on Jan 1, 2008 (gmt 0)

10+ Year Member



You can't package perls global variables with "my", you would use 'local' to change the value of the warnings flag for a specific block of code:


sub foo {
local $^W = 0;
...
}

The argument of less typing, -w versus "no warnings" is insignificant. See "Whats wrong with -w and $^W" on the perldoc website:

[perldoc.perl.org...]

PHP_Chimp

8:37 pm on Jan 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Nice, finally someone who can point me to something that explains why use warnings is better than -w.
Thanks.

perl_diver

12:23 am on Jan 3, 2008 (gmt 0)

10+ Year Member



You're welcome.