I'm wondering why
use warnings; use strict;
are not default in Perl. They're needed for every script. If someone (for good reason) needs to disable them, they should use no strict
and/or should use some command line argument (for one-liners).
Are there too many badly-written CPAN modules (using "badly" to mean without use strict
)? Or is it because this can break a lot of code already in production? I'm sure there is a reason and I would like to know it.
In 5.14 IO::File
is loaded automagically on demand, wouldn't it be possible to do something like that with these basic pragmas?
Strict and warnings are the mode for the Perl program. It is allowing the user to enter the code more liberally and more than that, that Perl code will become to look formal and its coding standard will be effective.
use warnings; When the warning pragma is used, the compiler will check for errors, will issue warnings against the code, and will disallow certain programming constructs and techniques. This pragma sends a warning whenever a possible typographical error and looks for possible problems.
The strict pragma checks for unsafe programming constructs. Strict forces a programmer to declare all variables as package or lexically scoped variables. Strict also forces specific syntax with sub, forcing the programmer to call each subroutine explicitly.
In perl (as in all programming languages), a "pragma" is a directive that specifies how the compiler (or in perl's case, the interpreter) should process its input. They are not part of the language per se, but are a sort of command-line option that tells the interpreter how to behave.
It's for backwards compatibility. Perl 4 didn't have strict at all, and there are most likely still scripts out there originally written for Perl 4 that still work fine with Perl 5. Making strict automatic would break those scripts. The situation is even worse for one-liners, many of which don't bother to declare variables. Making one-liners strict by default would break probably millions of shell scripts and Makefiles out there.
It can't be loaded automagically, because it adds restrictions, not features. It's one thing to load IO::File when a method is called on a filehandle. But activating strict unless the code did something prohibited by strict is meaningless.
If a script specifies a minimum version of 5.11.0 or higher (e.g. use 5.012
), then strict is turned on automatically. This doesn't enable warnings, but perhaps that will be added in a future version. Also, if you do OO programming in Perl, you should know that using Moose automatically turns on both strict
and warnings
in that class.
If you are on a modern Perl, say so, you just have to enable it. 5.12 applies strict
except for one-liners. It can't be default because of backward compatibility.
$ cat strict-safe?.pl use 5.012; $foo $ perl strict-safe\?.pl Global symbol "$foo" requires explicit package name at strict-safe?.pl line 2. Execution of strict-safe?.pl aborted due to compilation errors.
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