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
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;
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;
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;
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