Perl strict

What is the sake of using `use strict;` in perl codes? and how can avoid it?.TnX.

From perldoc strict: Perl pragma to restrict unsafe constructs

It's normally used to ensure that all variables/subs/references that you use are used in a clean manner, eg. variables are defined before access. And I don't think anyone should avoid it, as it reports a good deal of possible problems that you would waste time over debugging otherwise.

The "strict" pragma makes it an error to use various potentially unsafe or confusing code constructs. Type man 3 strict to find out which ones. Unless you have a very good reason for using such constructs, you should use "use strict". If you have a good enough reason. you can locally override the restrictions with (e.g.)

no strict "subs";

because pragmata are lexically scoped.

pludi and spirtle thanks and have a nice day :cool::b:.

If you don't use the strict pragma in a Perl programme all the variables you define (or rather introduce) become implicitly package globals that appear in the symbol table.
At first glance this might tremendously ease the coding (for the novice) but this also is hugely error prone, especially to typos.
So if you inadvertently misspell a variable without strict the Perl compiler would silently declare a new package global without the faintest sign of warning, but this most of the times is pretty useless or even detrimental and will break your programme logic since you intended to refer to some previously declared and defined variable.
Instead when you use strict the Perl compiler will immediately abort compilation and issue an error message hinting at the reason and line which violated the syntax.
Besides, it is considered bad programming style to use global variables,
and one should almost always use lexically scoped variables (those declared by my)
Therefore, even the seasoned Perl programmer should always have the use strict; line at the beginning at their code, and that is one vital marker that indicates good Perl code.

buffoonix thanks for this fundamental description.It's coool. :slight_smile:

Bah.

Most of perl these days is modularized, with variables accessed through AUTOLOAD functions or entries in hash tables, for which "use strict" is useless.

In my opinion, if you have to use "strict", you're probably using the wrong programming language.

Interesting opinion, you may be right, for I don't claim to be a Perl expert.
It only happens that I quite regularly attend Perl conferences and workshops where the acclaimed Perl elite meets and gives talks and presentations.
Almost all the sample code presented at these events sticks to use strict and there seems to be common (tacit?) agreement that it is considered good style.
I'm not sure if you would suggest that e.g. Damian Conway is using the wrong language?
At least in his Perl Best Practices in chapter 18 in the pragraph on Stricture it reads, Always use strict, followed by some explanation why you should do so, but also that you cannot always rely on that it will capture every mistake.
Finally, if I use a well tested and proven useful module (e.g. one of the better ones from CPAN) I think I would rely most of the times on that the module author(s) have done a decent job in at least eliminating those bugs that stricture would capture.
It is more for the wrapping code that I add where I would like to make use of this extra parachute because I know too well how easily even typos happen whose unnecessary debugging would waste far too much time.

I used 'strict' today on a script I've been working on for weeks. Had I used it earlier, I would have caught an unused-variable bug, but mostly it reported annoying stuff about unspecified-package variables.
Just thought I'd share.