Search code examples
perlwarningspragma

Should I turn on Perl warnings with the command-line switch or pragma?


Is there a difference between the two examples below for beginning a Perl script? If so, when would I use one over the other?

example 1:

#!/usr/bin/perl

use warnings;

example 2:

#!/usr/bin/perl -w

Solution

  • Using the switch will enable all warnings in all modules used by your program. Using the pragma you enable it only in that specific module (or script). Ideally, you use warnings in all your modules, but often that's not the case. Using the switch can get you a lot of warnings when you use a third party module that isn't warnings-safe.

    So, ideally it doesn't matter, but pragmatically it's often preferable for your end-users not to use the switch but the pragma.