Think about Loose Coupling | |
PerlMonks |
Thoughts on revisiting old codeby rinceWind (Monsignor) |
on May 16, 2002 at 10:00 UTC ( [id://166961]=perlmeditation: print w/replies, xml ) | Need Help?? |
The discussion on this thread has made me recollect some of my own experiences. I had been hacking Perl for 3 years before seeing the light and discovering use strict;. There are plenty of places on this site which extoll the virtues of strict.pm, so I will not be going into details. My initial experience was Waah! sooo many errors! But then I looked in detail at what the code was doing (all the code was 100% written by me - no cargo deliveries here :-), I realised that 99% of the errors could be removed just by inserting the keyword my. There was no need for any of the offending variables to be global. Also, I had mastered subs' argument passing and returning quite early on (thanks to the Camel 2nd edition and Sriram Srivanam's Advanced Perl Programming), which meant I never needed to pass in values to subs via global variables. Having a Comp sci and C programming background, I instinctively felt that global variables were ugly. I was thus able to iterate round the loop: Edit source.pl perl -c source.pl understand errorsThe process, uncovered some bugs which I had no idea existed. For example, the newbie trap Only $foo here is lexical. I was wondering at the time of writing the code originally, why $bar was retaining values between iterations. The remainder of the strict errors fell into 2 categories:
Having got a clean compile on strict, I then did the same iteration loop with -w. I recommend this approach - it's good for the soul. Also, I find that a consequence is that any new code I write, my fingers go on auto-pilot with use strict; even when coding a noddy example.
Back to
Meditations
|
|