in reply to Add use strict to this?
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
|
|---|