When I code, I try to make perl as hard on myself as possible, in order to catch bugs early. This is one reason why I, and alot of other people always use strict and -w. I find that when you code consistently in this environment, the amount of errors you produce goes down, as you slowly change your style to be cleaner.

One thing I do is put this at the top of all my code:

#!/usr/bin/perl -w BEGIN { use strict; #Always.. use Carp qw(verbose confess); #die verbosely, with stacktraces $SIG{__WARN__} = \&confess; #die, on warnings }

The main reason behind this is to make sure that there are no uninitialized value warnings, among other things, or else the program won't function at all. I find it forces me to use defined and exists as well before accessing values.

Also, by putting it inside the BEGIN block, I am ensuring it will happen before any other modules load or any other code is executed.

Does anyone think this is going too far, or that it could cause problems? If not, does anyone have any other general suggestions on how to make perl harder on myself?

Update: I have taken tilly's advice, and removed the code, and put it outside of a BEGIN block:

#!/usr/bin/perl -w use strict; use Carp qw(verbose confess); $SIG{__WARN__} = \&confess;

I still prefer to set the warn signal handler to confess, because I want my programs to die when modules or the code itself has unitilialized values or other warnings inside it. I find that it forces me not to ignore small or large problems, as a broken program/module will get fixed before an annoying one. (And you never know when that annoying program could be causing 10x the amount of time to fix than if it was done when the annoyances first started).


In reply to Re: Trivial style ideas by dkubb
in thread Trivial style ideas by tilly

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.