Do I need to use warnings when already using strict ones?

Enter the code as follows:

use strict; use warnings; 

Here use warnings; ?

+8
perl
Jun 21 '11 at 7:17
source share
6 answers

Yes, it is necessary.

use strict and use warnings do different things.

From the manual page of the strict module:

strict - Perl pragma to limit unsafe constructions

From perlrun (for -w ):

prints warnings about dubious constructs, such as variable names that are mentioned only once and scalar variables that are used before installation, overridden routines, undefined file descriptors or read-only file descriptors that you are trying to write, values ​​used as a number that does not look like numbers using an array, as if it were a scalar, if your routines recurs over 100 depths and countless many A lot of other things.

+8
Jun 21 '11 at 7:28
source share

Yes. strict guards against a very limited number of things. warnings warn you of a different and much wider set of problems.

+5
Jun 21 2018-11-11T00:
source share

The same question appeared here a few days ago: What security network do you use in Perl? . This link leads to a discussion of strict warnings, diagnostics, and other similar topics.

+4
Jun 21 '11 at 9:13
source share

Yes, consider:

 perl -le "use strict; my $f; my $z = $f*1" 

strict does not let you know that $ f is undefined, and adding warnings will be:

 perl -le "use strict; use warnings; my $f; my $z = $f*1" Use of uninitialized value $f in multiplication (*) at -e line 1. 

thus, advice to include both.

+3
Jun 21 '11 at 7:32
source share

This is not necessary in the actual meaning of the word. That is, the program does not require work from it. People often forget what they mean.

However, if your question is: “Do warnings be strictly resolved?”, The answer is no. You can see what reading the documentation strictly does.

warnings are often useful for indicating issues that you need to fix. However, Perl warnings do not need to be fixed, so your program may continue even if it issues warnings.

Some people will tell you to always use warnings, but they make such a rule, so they don’t need to think about it or explain to people who will not think about it. This is an unpopular position to say something other than “always use warnings”.

warnings are a tool for developers, and if people who see warnings don’t know what they represent and what to do with them, they are likely to just cause annoyance or confusion. I saw several examples when new pearls started to give new warnings for programs that then filled disks, since no one controlled the log files.

I have a much more subtle rule "use warnings when you do something about them, and do not use them if you do not want to."

I don’t even say this: "Always write without warning." There is a lot of code that I write that I will run exactly once, either from the command line, or in other situations where I don't care about sloppiness. I don’t like giving dirty warning code to other people, but I don’t get hung up on the little things that I do for myself.

+2
Jul 06 '11 at 18:18
source share

"What do you mean by fit?" this is my answer.

If you think strict and warnings are the same thing, you're wrong. Other people here gave very good answers regarding every pragma.

use warnings will generally make you a better coder. If training is important, then my answer will be “Yes.”

It will also help you avoid mistakes, facilitate understanding of mistakes.

In general, I would say that there are very few cases where you do not need to use both strict and warnings . I even use warnings in my one line, for example. > perl -we 'print "Hello world!"'

It takes a few seconds to enter, but this will save you from the extra hours of unnecessary debugging.

+1
Jun 21 '11 at 10:17
source share



All Articles