I remember seeing some comment somewhere that
use warnings;
use strict;
was preferable (rather than use'ing strict
first, as I was wont to do). Is my memory correct? Does the order matter and, if it does, in what way?
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.
The purpose of "use strict" is to indicate that the code should be executed in "strict mode". With strict mode, you can not, for example, use undeclared variables. The numbers in the table specify the first browser version that fully supports the directive. You can use strict mode in all your programs.
If you have such an unrestrictedly typed code, that is used variables without declaring. One variable declared within some function/scope and used from somewhere else(it will be undeclared there) and you can't rewrite/change them, then you should not go for "use strict;" mode because it will break the code.
I can't see how there is any technical reason for the order being significant. I always put use strict
first as it happens but for no reason other than I've always done that!
Simple: If you do use strict
before you do use warnings
, you won't get a warning if you're using use strict
wrong. DUH!
Actually, I've always done it the other way. The anal retentive in me made me put the two pragmas alphabetically. I never knew there was a reason to do it one way or another.
Heck, perldoc use shows placing use strict
before use warnings
, and that's what I've seen in most of the official Perl examples.
I suspect there's a cultural reason. Originally in Perl, to use warnings, you use to use
#! /usr/bin/perl -W
on the first line of your program. The use strict
pragma came later, and only later was use warnings
a pragma. Thus, programs (for a while) were written as:
#! /usr/bin/perl -W
use strict;
When the -W
parameter fell out of fashion, you simply added use warnings
right below the shebang:
#! /usr/bin/perl
use warnings;
use strict;
One of the things I like about Stackoverflow is that you can learn new things you never knew before. I'd love to see if someone has an actual reason why it should be one way and not another.
I thought, maybe, on Perl 5.8.9 and older where strict
does not warn when you use Strict
on case insensitive file systems, having warnings
first would help. I built 5.8.9 on my Windows XP system and it does not:
C:\Temp> cat t.pl
use warnings;
use Strict;
C:\Temp> c:\opt\perl-5.8.9\bin\perl.exe t.pl
C:\Temp>
That was a shot in the dark. I am led to conclude that so long as there are no intervening statements, the order is immaterial.
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