Jerald Sheets wrote:
On Mar 8, 2009, at 1:29 PM, Ron Bergin wrote:
On Mar 4, 4:46 am, que...@gmail.com (Jerald Sheets) wrote:

#!/usr/bin/perl -w

It's better to use the warnings pragma, instead of the -w switch

<snip>

... on the -w switch, here are Damian's words:

<quote>
18.8. Warnings

Always turn on warnings explicitly.

If you're developing under Perl 5.6 or later, always use warnings at the start of each file. Under earlier versions of Perl, always use the -w command-line flag, or set the $WARNING variable (available fromuse English) to a true value.

Perl's warning system is invaluable. It can detect more than 200 different questionable programming practices, including common errors like using the wrong sigil on an array access; trying to read an output stream (or vice versa); leaving parentheses off ambiguous declarations; runaway strings with no closing delimiter; dyslexic assignment operators (=-, =+, etc.); using non-numbers as numbers; using | instead of ||, or || instead of or; misspelling a package or class name; mixing up \1 and $1 in a regex; ambiguous subroutine/function calls; and improbable control flow (e.g., returning from a subroutine via a call to next).
</quote>

He appears to insinuate they are interchangeable with the phrase:

"always use the -w command-line flag, or set the $WARNING variable (available fromuse English) to a true value."

No he does not. You failed to re-quote the "Under earlier versions of Perl".

It's true that for a program to be compatible with pre 5.6 Perl versions, the dynamically scoped warnings feature is your only choice. Otherwise you can choose, and as long as you know the differences, and make your choice based on what you want to achieve, all is fine IMO. As long as you do not know all the implications, you'd better believe the experienced Perlers when they say that "use warnings;" is typically the best choice.

Another note on this... I just perldoc'ed it to see what it had to say:

DESCRIPTION
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. See perllexwarn for more information.

If the pragma is limited to the enclosing block but the flag is global *AND* it is considered best practice to remove the pragma when distributing your program:

<quote>
ote that it may still be appropriate to comment out the use warnings line when your application or module is deployed, especially if non-technical users will interact with it, or if it will run in a CGI or other embedded environment. Issuing warnings in these contexts can needlessly alarm users, or cause server errors.

Don't remove the use warnings completely, though; when something goes wrong, you'll want to uncomment it again so the reinstated warnings can help you locate and fix the problem.
</quote>

I am failing to see where this would be preferable except in the case of also using the "use English" pragma or many of the other options that would be limited to the enclosing block.

"use English" has nothing to do with it. The dynamically scoped warnings feature can be enabled via the -w switch or by assigning a true value to the $^W variable directly. $WARNING is just an alias to $^W under "use English".

If you really want to understand, I suggest that you read perllexwarn carefully.

However, not everything in perllexwarn is true. Unlike the impression that that piece of the docs gives you, it's fully possible to combine the dynamically scoped warnings feature with "no warnings":

C:\home>type test.pl
$^W = 1;
$x = undef;
sub mysub {
    no warnings;
    print $x;    # line 5
}
print $x;        # line 7
mysub();

C:\home>test.pl
Use of uninitialized value $x in print at C:\home\test.pl line 7.

C:\home>

Unless "no warnings" had been effective, line 5 would also have generated a warning.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to