I read that it is better to use warnings;
instead of placing a -w
at the end of the shebang.
What is the difference between the two?
use strict pragma also works in the same way as use warnings but the only difference is that the strict pragma would abort the execution of program if an error is found, while the warning pragma would only provide the warning, it won't abort the execution.
Strict and Warning are probably the two most commonly used Perl pragmas, and are frequently used to catch “unsafe code.” When Perl is set up to use these pragmas, the Perl compiler will check for, issue warnings against, and disallow certain programming constructs and techniques.
There is a strange scalar variable called $_ in Perl, which is the default variable, or in other words the topic. In Perl, several functions and operators use this variable as a default, in case no parameter is explicitly used.
use strict; The use strict pragma forces the developer to code in a manner that enables the code to execute correctly, and it makes the program less error-prone. As an example, the use strict pragma forces the developer to declare variables before using them. We can declare variables with the keyword my in Perl.
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 and the list of built-in warning categories.
–
warnings
documentation
The advantage of use warnings
is that it can be switched off, and only affects the immediate scope.
Consider for example a module that uses operations that would emit warnings:
package Idiotic;
sub stuff {
1 + undef;
}
Then we get a warning if we do
#!perl -w
use Idiotic; # oops, -w is global
Idiotic::stuff();
but don't get any warning with
#!perl
use warnings; # pragmas are scoped, yay!
use Idiotic;
Idiotic::stuff();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With