Why use strict warnings and warnings?

It seems to me that many issues in the Perl tag can be resolved if people use:

use strict; use warnings; 

I think some people think that they are akin to training wheels or unnecessary complications, which is clearly not the case, as they are used by even very experienced Perl programmers.

It seems that most people who own Perl always use these two pragmas, while those who benefit most from using them rarely do. So, I thought it would be nice to link the question when encouraging people to use strict and warnings .

So, why warnings Perl developer use strict and warnings ?

+92
perl
Nov 05 2018-11-11T00:
source share
8 answers

To get started, use strict; (and, to a lesser extent, use warnings; ) helps to find typos in variable names. Such experienced programmers make such mistakes. A common case is to forget to rename an instance of a variable when cleaning or refactoring code.

Using use strict; use warnings; use strict; use warnings; breaks many errors before they are caught otherwise, which will simplify the search for the root causes of errors. The root cause may be the need for error checking or verification, and this can happen regardless of the skill of the programmer.

What is the use of Perl warnings is that they are rarely fake, so they can be used without cost.




Related reading: Why use my ?

+72
Nov 06 '11 at 12:00 a.m.
source share

Apparently, use strict should (should) be used when you want to force perl to be used for code that could force the declaration to be explicit for lines and subtypes ie, or use links with caution. Note: if there are errors, using the line cancels execution, if used.

While use warnings; helps you find input errors in the program, for example, you missed the semicolon, you used "elseif" and not "elsif", you use outdated syntax or function, however. Note: using warnings will provide warnings and continue execution, i.e. does not cancel execution.

In any case, it would be better if we move on to the details, which I will discuss below

From perl.com (my favorite):

use strict 'vars';

which means you should always declare variables before using them.

If you do not declare, you are likely to receive an error message for an undeclared variable

The global symbol "$ varablename" requires an explicit package name in the scriptname.pl 3 line

This warning means that Perl is not clear what a variable is. Therefore, you need to clearly indicate your variables, which means either declare them with my so that they are limited to the current block, or refer to them with their full name (for ex: $ MAIN :: variablename).

So, a compile-time error is triggered if you try to access a variable that has not met at least one of the following criteria:

  • Predefined by Perl itself, such as @ ARGV,% ENV, and all global punctuation variables such as $. or $ _.

  • Declared with ours (for global) or my (for lexical).

  • Imported from another package. (Using vars pragma pushes import, but uses ours instead.)

  • Fully qualified using package name and double column package separator.

use strict 'subs';

Let's consider two programs

 # prog 1 $a = test_value; print "First program: ", $a, "\n"; sub test_value { return "test passed"; } Output: First program result: test_value # prog 2 sub test_value { return "test passed"; } $a = test_value; print "Second program: ", $a, "\n"; Output: Second program result: test passed 

In both cases, we have test_value (), and we want to put its result in $ a. And yet, when we run two programs, we get two different results:

In the first program, at the point we get to $a = test_value; , Perl does not know any test_value () elements, and test_value is interpreted as the string test_value. In the second program, the definition of test_value () precedes the line $a = test_value; . Perl considers test_value to be a subheading.

The technical term for isolated words, such as test_value, which can be subs and can be strings, depending on the context, by the way, is bareword. Handling Perl barewords can be confusing, and it can cause a program error.

The error we encountered in our first program. Remember that Perl does not expect test_value() searched, since it has not yet seen test_value (), it assumes you need a string. Therefore, if you use strict subs; , this will make this program die with an error:

Bareword "test_value" is not allowed while strict subtitles are used. / a 6-strictsubs.pl line 3.

The solution to this error will be 1. Use parentheses to make it clear that you are calling sub. If Perl sees $ a = test_value () ;,
2. Declare your unit before its first use.

 use strict; sub test_value; # Declares that there a test_value() coming later ... my $a = test_value; # ...so Perl will know this line is okay. ....... sub test_value { return "test_passed"; } 

3. And if you want to use it as a string, quote it.

So, this stricture forces Perl to treat all words as syntax errors. * Spoken word - any bare name or identifier that has no other interpretation caused by the context. (A context often forces an adjacent keyword or token, or by predefining the word in question.) * So, if you want to use it as a string, quote it, and if you want to use it as a function call, imagine it or use parentheses.

Bugs are dangerous due to this unpredictable behavior. use strict; (or use strict 'subs';) use strict; (or use strict 'subs';) makes them predictable because bare words that can cause strange behavior in the future will cause your program to die before they can damage

There is one place where you can use open words, even if you turned on strict subtitles: when you assign hash keys.

 $hash{sample} = 6; # Same as $hash{'sample'} = 6 %other_hash = ( pie => 'apple' ); 

Bars in hash keys are always interpreted as strings, so there is no ambiguity.

use strict 'refs';

This creates a runtime error if you use symbolic links, intentionally or otherwise. A value that is not a hard link is then treated as a symbolic link. That is, the link is interpreted as a string representing the name of the global variable.

 use strict 'refs'; $ref = \$foo; # Store "real" (hard) reference. print $$ref; # Dereferencing is ok. $ref = "foo"; # Store name of global (package) variable. print $$ref; # WRONG, run-time error under strict refs. 

Use warnings

This lexically limited pragma allows you to flexibly manage Perl's built-in warnings, both those emitted by the compiler, and from the runtime system.

From perldiag :

Thus, most of the warning messages from the classifications below, namely W, D, and S, can be controlled using the warnings pragma.

(W) Warning (optional)
(D) Deviation (enabled by default)
(S) Serious warning (enabled by default)

I have listed some warning messages that are often found below classifications. For more information about them and other posts, contact perldiag

(W) Warning (optional):

Missing argument in% s
Missing argument -% c
(Did you mean &% s instead?)
(Did you mean "local", not "ours"?)
(Did you mean $ or @ instead of%?)
'% s' is not a reference to the code length () is used on% s
Invalid _ number

(D) Assignment (enabled by default):

defined (@array) is deprecated
certain (% hash) is deprecated
Deprecated use of my () in a false conditional expression $ # is no longer supported

(S) Serious warning (enabled by default)

elseif must be elsif
% s found where the operator was expecting
(Missing statement before% s?)
(Missing semicolon on previous line?)
% s is never entered
Operator or semicolon missing until% s
Priority issue: open% s should be open (% s)
Prototype mismatch:% s vs% s
Warning: using '% s' without parentheses is ambiguous
Cannot open% s:% s

+27
Feb 12 '13 at 8:31
source share

These two pragmas can automatically identify errors in your code.

I always use this in my code:

 use strict; use warnings FATAL => 'all'; 

FATAL causes the code to die from warnings, as strict does.

For more information, see Strengthen Use of Alerts FATAL => 'all';

Also ... Rows, according to Seuss

+10
Nov 05 '11 at 23:50
source share

There's a good stream on perlmonks about this question.

The main reason, obviously, is that strict and warning messages greatly help you make mistakes and help you debug.

+9
Nov 05 '11 at 23:21
source share

Source :: Various blogs

Usage will export the functions and variable names into the main namespace by calling the import () function.

A pragma is a module that affects some aspect of compilation time or perl.Pragmas runtime gives hints to the compiler.

Use warnings - perl complaints about variables used only once, incorrect conversion of strings to numbers,. Trying to write to files that do not open .it occurs at compile time. Used for telltale warnings.

Use string strict - declare variables scope. It is used to set some kind of discipline in a script. If only words are used in the code, they are interpreted. All variables must be set for both mine and ours.

+3
Jul 01 '13 at 7:35
source share

The "use strict" directive tells Perl to do extra checking when compiling your code. Using this directive will save you time debugging your Perl code because it detects common coding errors that you might otherwise ignore.

+1
Jun 29 '15 at 13:43 on
source share

Strictly and with warnings, make sure your variables are not global.

It is much neat to have variables unique to individual methods, rather than keeping track of every variable name.

$ _, or there is no variable for some functions, it may also be useful to write more compact code faster.

However, if you do not use strict and warning messages, $ _ becomes global!

0
Apr 6 '15 at
source share
 use strict; use warnings; 

Strictly and warnings - this is the mode for the perl program. This allows the user to enter the code more liberally and moreover, that the perl code will look formal and its coding standard will be effective.

warnings mean the same as -w in the perl shebang line, so it will give you warnings generated by the perl program, will display in the terminal

0
Jun 28 '16 at 12:24
source share



All Articles