Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a reason to use warnings before use strict?

Tags:

perl

pragma

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?

like image 366
Sinan Ünür Avatar asked Jun 20 '11 13:06

Sinan Ünür


People also ask

What are some differences between strict and warnings?

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.

Why use strict and warnings Perl?

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.

Why we use use strict?

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.

When it is a good time not use use strict in Javascript?

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.


3 Answers

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!

like image 113
Roger Avatar answered Oct 20 '22 08:10

Roger


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.

like image 26
David W. Avatar answered Oct 20 '22 07:10

David W.


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.

like image 31
Sinan Ünür Avatar answered Oct 20 '22 07:10

Sinan Ünür