8

Code as follows:

use strict;
use warnings;

Is use warnings; necessary here?

new_perl
  • 7,345
  • 11
  • 42
  • 72
  • I have seen this too often. My new response will be: http://joelslinux.blogspot.com/2011/06/use-strict-and-warnings.html – Joel Berger Jun 21 '11 at 15:22
  • 2
    And honestly I am surprised that the community has seen fit to vote this up so high when http://stackoverflow.com/search?q=%5Bperl%5D+strict+warnings shows that this has been mulled over a few times. – Joel Berger Jun 21 '11 at 15:29

6 Answers6

8

Yes, it's necessary.

use strict and use warnings do different things.

From the strict module's manpage:

strict − Perl pragma to restrict unsafe constructs

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 being set, redefined subroutines, references to undefined filehandles or filehandles opened read-only that you are attempting to write on, values used as a number that don't look like numbers, using an array as though it were a scalar, if your subroutines recurse more than 100 deep, and innumerable other things.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
  • 1
    is there an example that shows `use warnings` does additional things? – new_perl Jun 21 '11 at 07:30
  • 1
    You should quote [perldiag](http://p3rl.org/diag) and [perllexwarn](http://p3rl.org/lexwarn) for `use warnings` instead of perlrun `-w`. – daxim Jun 21 '11 at 09:18
  • @daxim useful references indeed, but don't have a concise summary like the one I quoted from perlrun. – Alnitak Jun 21 '11 at 09:57
5

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

ysth
  • 96,171
  • 6
  • 121
  • 214
5

This same question came up a few days ago here: Which safety net do you use in Perl?. That link leads to a discussion of strict, warnings, diagnostics, and other similar topics.

Community
  • 1
  • 1
DavidO
  • 13,812
  • 3
  • 38
  • 66
3

Yes, consider:

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

strict doesn't let you know that $f is undefined, while adding warnings will:

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

thus the advice to enable both.

Alex
  • 5,863
  • 2
  • 29
  • 46
  • I didn't get `Use of uninitialized value $f in multiplication (*) at -e line 1.` under perl v 5.8.8 – new_perl Jun 21 '11 at 07:35
  • Are you sure? Just tested it on 5.8.8 and 5.10.1. Both give the same error message. Do you perhaps have a modified build of Perl? Or perhaps you forgot to `use warnings;`. – Htbaa Jun 21 '11 at 09:45
  • @new_perl, then you have a very broken Perl installation. (I think 5.8.8 didn't mention `$f` in the warning, but it did issue the warning.) – ikegami Jun 21 '11 at 18:26
2

It's not necessary in the actual meaning of that word. That is, a program doesn't require it to operate. People often forget what necessary means.

However, if your question is "Does strict enable warnings?", the answer is no. You can see what strict does by reading its documentation.

warnings are often useful in pointing out problems that you should fix. However, Perl's warnings don't require you to fix them, so your program can continue even though it emits warnings.

Some people will tell you to always use warnings, but they make that sort of rule so they don't have to think about it or explain it to people who won't think about it. It's an unpopular position to say anything other than "always use warnings".

warnings are a tool for developers, and if the people who see the warnings aren't going to know what they are or what to do about them, they are probably just going to cause annoyance or confusion. I've seen a few instances where new perls started to emit new warnings for programs, which then filled up disks as no one was monitoring the log files.

I have a much more nuanced rule "use warnings when you'll do something about them, and don't use them if you won't".

I don't even go as far as saying as "Always write warning-free code". There's plenty of code that I write that I will run exactly once, or from the command line, or in other situations where I don't care about the sloppiness. I don't like giving warning-dirty code to other people, but I don't obsess over it for little things I do for myself.

brian d foy
  • 129,424
  • 31
  • 207
  • 592
  • harsh down votes on those whose views you don't agree with. In my case the answer was based on interpreting the question as meaning _"is `use warnings` a subset of `use strict`?"_ - i.e. does `use strict` obviate the need to `use warnings` at all. – Alnitak Jul 06 '11 at 18:41
1

"What do you mean by necessary?" is my reply.

If you think strict and warnings are the same thing, you are wrong. Other people here have given very good answers as to each pragma does.

use warnings will in general make you a better coder. If learning is important, then my answer would be "Yes."

It will also help you avoid bugs, make your errors easier to understand.

In general, I would say that there are very few times when it is warranted to not use both strictand warnings. I even use warnings in my one-liners, e.g. > perl -we 'print "Hello world!"'

It takes a few seconds to type in, but will save you hours of needless work debugging.

TLP
  • 66,756
  • 10
  • 92
  • 149