13

With perl I almost always use:

use strict;
use diagnostics;

I suggested "use diagnostics" instead of use warnings; here and I received some negative feedback. So, now I'm thinking:

Is there any problem with use diagnostics;?

Community
  • 1
  • 1
cirne100
  • 1,558
  • 2
  • 12
  • 24

4 Answers4

17

diagnostics isn't a lexical pragma like warnings. Instead it sets the global $^W variable (like using the -w flag), which enables warnings for all code, whether it wants it or not, and whether you wrote it or not. That's kind of rude. diagnostics is also silly to use in production. I would recommend using warnings and then just piping your warnings to splain if you need an explanation.

hobbs
  • 223,387
  • 19
  • 210
  • 288
9

use diagnostics will slow down the execution of your code more than use warnings. It's fine to switch it on if you're getting a warning that you don't understand during development, but it's best to switch it off when you're done.

Toto
  • 89,455
  • 62
  • 89
  • 125
thomson_matt
  • 7,473
  • 3
  • 39
  • 47
8

The diagnostics pragma is a development tool, and one that you don't need often. Use it on an as-needed basis:

$ perl -Mdiagnostics myprog

Enabling the pragma with the command-line -M switch means you don't have to remember to go back and remove a test-only use diagnostics line from your code.

Greg Bacon
  • 134,834
  • 32
  • 188
  • 245
0

If you have not yet upgraded to perl version 5.12, diagnostics has a bug which masks a specific warning. The bug has been fixed in 5.12 (see perl512delta):

diagnostics no longer suppresses Use of uninitialized value in range (or flip) warnings. [perl #71204]

Once I became aware of the bug, I went back and commented use diagnostics; out of all my code. If I get warning messages, I uncomment it temporarily, but always go back and comment it out again.

toolic
  • 57,801
  • 17
  • 75
  • 117