Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

strict, warnings and diagnostics pragmas

by tomazos (Deacon)
on Aug 03, 2001 at 16:54 UTC ( #101956=perlmeditation: print w/ replies, xml ) Need Help??

At the beginning of every Perl script you write put:

use strict; use warnings; use diagnostics;

This will among many good things make sure you declare everything in the right namespace, give you extensive warnings about weird things you write (which 9 times out of 10 will be bugs) and when your script encounters an error (compile-time or run-time) it will dump very useful information about what exactly went wrong.

Do not take these pragmas out when your script goes into production unless you expect your script to be very processor intensive or for it to be called very frequently.

This is so that the next time you or someone else makes a "small" change to the script, the bugs they have introduced can be caught quickly with the aid of the pragmas (without having to remember to put them back in).

Yeah, I know. We've heard it before. :) Now you've heard it again.

Comment on strict, warnings and diagnostics pragmas
Download Code
Re: strict, warnings and diagnostics pragmas
by pmas (Hermit) on Aug 03, 2001 at 17:12 UTC
    I am using slightly different approach:
    I have different configuration files in development and production, so I can also have use CGI::Carp qw(fatalsToBrowser); in development and not in production. Development environment has also set DEBUG variable on, so running the same script in development yields a lot of info about what is going on, without need to comment it out for production. Basically, I look at debug printouts as "printable comments". And valid comments never hurt, right?

    pmas
    To make errors is human. But to make million errors per second, you need a computer.

Re: strict, warnings and diagnostics pragmas
by Hofmator (Curate) on Aug 03, 2001 at 17:30 UTC

    I don't think use diagnostics; is necessary, it just gives you some longer warning/error descriptions from perldiag - and blows up your startup time. I'd say nice for development if you need it, but not in production code.

    As a small side remark, diagnostics sets $^W = 1 so use warnings is not really necessary then ...

    -- Hofmator

      I agree with Hofmator here. use diagnostics is a very good idea ... for a person still learning Perl. For those of us who've been with it hardcore for over a year, the extra verbiage isn't worth the extra execution time. (And extra RAM, though that's usually less of an issue.)

      I would add use 5.6.0; to that list. This makes sure that our and other nifty-pretties from 5.6.0+ are supported. If they're not, you'll know immediately why.

      My header almost always looks like:

      use 5.6.0; use strict; use warnings;
      Then, I can always comment out warnings, if I wanted to.

      ------
      /me wants to be the brightest bulb in the chandelier!

      use warnings; and perl -w are not the same thing.

        use warnings; and perl -w are not the same thing.
        I'm aware of that, the different scoping, the possibility to use warnings::register; ... but the way I understand it, use warnings; is not reasonable after setting $^W = 1; # equivalent to perl -w
        which is done by use diagnostics;. Please correct me if I'm overlooking something here...

        -- Hofmator

Re: strict, warnings and diagnostics pragmas
by xphase_work (Pilgrim) on Aug 03, 2001 at 18:03 UTC
    As dragonchild said here, make sure that if you include the use warnings; pragma, then also include the use 5.6.0; pragma. The use warnings; pragma is not supported before perl version 5.6.0.

    --xPhase

Re: strict, warnings and diagnostics pragmas
by tachyon (Chancellor) on Aug 03, 2001 at 18:32 UTC

    You should not use diagnostics in production as this requires the loading of a very large chunk of useless info. Use warnings triggers the warnings. Use diagnostics only *expands* those warnings, thus use diagnostics is useful only if you don't understand what the warnings generated by use warnings mean.

    When you use diagnostics perl loads a large quantity of data each and every time you run the script. If there are no warnings this will never be used. As already noted if there are warnings this will only be useful if you don't understand the warnings. You can get the same info from perlman:lib:diagnostics. There is extensive discussion of this here Use strict warnings and diagnostics and Use strict warnings and diagnostics or die

    It is also worth a look at use warnings vs. perl -w as use warnings is a perl 5.6+ feature so will not work on the still common 5.005 or earlier. As noted -w works on Win32 or any other system as Perl will look at the shebang regardless of whether the OS does.

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://101956]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2014-09-21 04:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (166 votes), past polls