ironpaw, I had an example the other day which made my code completely broken because I didn't use strict.

It's a beginner's error but I'll own up to it anyway.

I have a subroutine which uses Date::Manip to find the day of the week for a series of dates. But when I ran it over my dates, everything was a Tuesday! I'd had a long day and this was driving me crazy.

My mistake? My sub went like this:

sub tell_me_the_day { $date = shift(); # a scalar, say 2003-07-01 ### chop the $date scalar up with a regex then push (@date_parts, $1,$2,$3); ### using Date::Manip; return &Date_DayOfWeek( $date_parts[1], $date_parts[2], $date_parts[3] ); }

Now, I'm doing that a kludgey way, but the point is, I was sitting there thinking "It looks fine! What's wrong?" when strict would have told me what was wrong.

Obviously, when this sub is run ten times, on ten $date scalars, it pushes more and more items onto the end @date_parts, but keeps sending the first three to Date::Manip.

What I wanted was a new fresh @date_parts array every time, so I should have said "push (my @date_parts, $1,$2,$3);"



“Every bit of code is either naturally related to the problem at hand, or else it's an accidental side effect of the fact that you happened to solve the problem using a digital computer.”
M-J D

In reply to Re: Use Strict by Cody Pendant
in thread Add use strict to this? by ironpaw

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.