strict does exactly three things: use strict 'refs'; causes a runtime error if you attempt to use symbolic references. use strict 'vars'; generates a compiletime error if you attempt to access a variable that was neither predeclared nor fully qualified.. use strict 'subs'; generates a compiletime error if you attempt to use a bareword that is not a subroutine name or filehandle (a slight oversimplification). use strict; gives you all three of those behaviors, and that's generally the minimum standard that modern Perl users prefer to code to.

The reason for using "my" (lexical) variables is that they facilitate compartmentalization and scoping, which are fundamental principles for structured programming. If you wish to enjoy the benefits provided by structured programming principles, you will appreciate that 'strict' helps you to enforce the use of lexically scoped variables. Regardless of whether you want to use compartmentalization techniques, you should still enjoy the benefit that enforced pre-declaration of variables provides in preventing typos from going undiscovered until a bug turns up.

A significant step toward becoming a good programmer is learning discipline. It may seem easy to pass values around a script globally, to not bother with parameter passing, and so on. ...and for fifteen or so lines of code, that probably is easier (unless doing so goes so much against habit that it feels harder). But for some number of lines of code greater than fifteen or so, the spaghetti nightmare that comes with undisciplined programming techniques will begin to make the task much harder than it needs to be. It turns out that developing good habits early on helps to make simple things easy, and hard things possible, which is a primary goal of programming with Perl.

Even someone who doesn't care about becoming a "good programmer" should care about putting more effort into something than necessary. Good programmers have learned that discipline reduces the overall effort required to accomplish any non-trivial programming task. The fact that discipline makes the job easier is universally applicable; it works not only for good programmers, but also for novices -- novices often just don't realize that it can help, and consequently end up working too hard.

So this is your moment! Take that first step toward making the task easier; program with discipline; use strict;. (But equally important, learn why you are using it.)


Dave


In reply to Re: Question on "my" in the variables by davido
in thread Question on "my" in the variables by Anonymous Monk

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.