what are two different ways to turn on warnings in perl?
31.22. use warnings
use warnings; # same as importing "all" no warnings; # aforementioned as unimporting "all" use warnings::register; if (warnings::enabled()) { warnings::warn("some alarm"); } if (warnings::enabled("void")) { warnings::warn("void", "some alarm"); }
This lexically scoped pragma permits flexible command over Perl's built-in warnings, both those emitted by the compiler besides as those from the run-time system.
One time upon a time, the only control you had in Perl over the treatment of warnings in your program was through either the -due west command-line option or the $^W variable. Although useful, these tend to be all-or-cipher affairs. The -westward option ends up enabling warnings in pieces of module code that y'all may non take written, which is occasionally problematic for you and embarrassing for the original author. Using $^W to either disable or enable blocks of lawmaking can be less than optimal because it works just during execution fourth dimension, not during compile time.[3] Another outcome is that this program-wide global variable is scoped dynamically, non lexically. That means that if you enable it in a block and so from there telephone call other lawmaking, yous over again risk enabling warnings in lawmaking non developed with such exacting standards in mind.
[three]In the absence of Begin blocks, of course.
The warnings pragma circumvents these limitations by being a lexically scoped, compile-time mechanism that permits finer command over where warnings tin or tin can't be triggered. A bureaucracy of warning categories (see Figure 31-1) has been defined to allow groups of warnings to be enabled or disabled in isolation from one another. (The verbal categorization is experimental and subject to change.) These categories tin exist combined by passing multiple arguments to use or no:
use warnings qw(void redefine); no warnings qw(io syntax untie);
Figure 31.1. Perl's alarm categories
If multiple instances of the warnings pragma are active for a given scope, their furnishings are cumulative:
To make fatal errors of all warnings enabled by a particular warnings pragma, employ the discussion FATAL at the front of the import list. This is useful when you would prefer a certain condition that normally causes simply a alarm to abort your program. Suppose, for example, that you lot considered it so improper to apply an invalid string as a number (which ordinarily produces a value of 0) that you want this brazen act to kill your plan. While you're at it, you decide that using uninitialized values in places where real string or numeric values are expected should also exist cause for firsthand suicide:use warnings "void"; # Only "void" warnings enabled. ... use warnings "io"; # Both "void" and "io" warnings now enabled. ... no warnings "void"; # But "io" warnings now enabled.
Now if either $y or $z is uninitialized (that is, holds the special scalar value, undef), or if they comprise strings that don't cleanly convert into numeric values, instead of going merrily on its mode, or at most issuing a pocket-sized complaint if you had -w enabled, your programme volition now raise a exception. (Recall of this as Perl running in Python mode.) If you aren't trapping exceptions, that makes it a fatal mistake. The exception text is the same as would unremarkably announced in the warning message.{ use warnings FATAL => qw(numeric uninitialized); $ten = $y + $z; }
The warnings pragma ignores the -w command-line switch and the value of the $^W variable; the pragma's settings take precedence. However, the -Westward command-line flag overrides the pragma, enabling full warnings in all lawmaking inside your programme, even code loaded with do, require, or utilise. In other words, with -Due west , Perl pretends that every block in your program has a utilise warnings 'all' pragma. Remember of it every bit a lint(1) for Perl programs. (But see also the online documentation for the B::Lint module.) The -10 command-line flag works the other fashion around. It pretends that every block has no warnings 'all' in outcome.
Several functions are provided to assist module authors who want to make their module'south functions behave like built-in functions with respect to the lexical scoping of the caller (that is, and then that users of the module can lexically enable or disable warnings the module might issue):
- warnings::register
-
Registers the current module name as a new category of warnings, so that users of your module can plough off warnings from it.
- warnings::enabled( CATEGORY )
-
Returns true if the warnings category CATEGORY is enabled in the lexical scope of the calling module. Otherwise, it returns simulated. If CATEGORY is non supplied, the electric current package name is used.
- warnings::warn( CATEGORY, Message )
-
If the calling module has not set CATEGORY to "FATAL", prints Bulletin to STDERR. If the calling module has set CATEGORY to "FATAL", prints Message to STDERR, so dies. If CATEGORY is not supplied, the electric current parcel name is used.
Copyright © 2002 O'Reilly & Associates. All rights reserved.
jonathanthads1976.blogspot.com
Source: https://docstore.mik.ua/orelly/perl4/prog/ch31_22.htm
0 Response to "what are two different ways to turn on warnings in perl?"
Post a Comment