perl

Why are use warnings; use strict; not default in Perl?


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?


Solution

  • 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. Similarly, with use 5.036 or higher, warnings are turned on automatically. Also, if you do OO programming in Perl, you should know that using Moose automatically turns on both strict and warnings in that class.