in reply to Common uses of "use"

use strict; is pretty ubiquitous but I don't even use warnings; all that much (I use "#!/usr/bin/perl -w" a lot).

The point of 'use warnings' was that somebody got annoyed with some module where the author didn't test that their module code didn't generate warnings. But it is certainly less than perfect on a lot of other points.

One big problem is that a lot of warnings are generated more by data than by code. For example:

use warnings; use NotMy::Module qw< setsomestring >; my $input= <STDIN>; setsomestring( $input );

If NotMy/Module.pm doesn't contain use warnings;, then my use warnings; above is powerless to tell me that I've failed to check for an end-of-file and have just passed undef to a function that isn't meant to deal with undef (and just acted like I passed it an empty string which is a bug and something I wanted to know about).

The converse is also a problem:

no warnings; use NotMy::Module qw< setsomestring >; my $input= <STDIN>; setsomestring( $input );

Here, "no warnings;" means that I'm coding in a mode where I don't want to have to write a bunch of obnoxious trivia like $input= '' if ! defined $input; all over the place and I actually want end-of-file to result in setsomestring() acting like I gave it an empty string. And yet, the new version of NotMy/Module.pm says use warnings; so I still get annoyed by those way-too-common "undef" warnings.

The better (but in-a-perfect-world) solution for the original problem of people who don't test whether their modules generate warnings (even when never given data by the module user that could be blamed for the warning) would actually be to get such module authors to mark their modules with no warnings;. That requires a perfect world because people who don't care about warnings aren't likely to insert no warnings; for the sake of the rest of us.

Luckily for me, the ubiquity of 'use warnings' in the hive mind means that I almost never run into the original problem these days. So my Perl scripts tend to start with "#!/usr/bin/perl -w" and not to 'use warnings'. If I find a warning-generating module, then I deal with that, but I rarely have to.

My modules tend to also not 'use warnings' but my module test scripts also start with #!/usr/bin/perl -w so I try to make sure that my modules don't generate warnings. But I let the users of my modules decide whether or not they want to get warnings when they use my modules.

(But, no, that isn't a perfect solution, either.)

- tye        

Replies are listed 'Best First'.
Re^2: Common uses of "use" (warnings?)
by mjscott2702 (Pilgrim) on Oct 28, 2010 at 08:49 UTC
    I also typically do a "perl -wc" check on the script to identify issues between the keyboard and chair. However, I usually have a use warnings statement in the code itself, which complains a lot with data-related issues e.g. string concatenation with un-initialised values.

    So, given that I perform the integrity check on the script first, is including this pragma as a matter of habit actually doing anything for me, other than generating lots of data-related warnings?

    Obviously, there ARE situations when you want to get these warnings, depending on the situation, but I'm leaning towards not including by default - or maybe, like diagnostics, using it when initially testing out the script, but then comment it out?

      Obviously, there ARE situations when you want to get these warnings,

      Just to be clear, I usually use #!/usr/bin/perl -w (as the first line of each script) which means that I get more warnings than I would if I used use warnings;.

      Users of a script usually don't have many ways of giving the script undefined values so the line between "script user" and "script" is not the same as the line between between "module user" and "module" with regard to warnings. If I test a script well, then I usually won't leave it up to the script user to launch via "perl -w ..." in order to get warnings.

      However, for scripts that get run other than by other either developers or IT workers, I do indeed sometimes leave warnings turned off when the script is deployed. Warnings seen by a non-technical user or written to a Production log file are very often worse than useless. The lower the odds of "the warning making it back to a developer along with sufficient information for the cause of the warning to be identified so the code can be fixed to eliminate the warning (and perhaps a bug that the warning detected)", the less value there is to leaving warnings enabled.

      - tye