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 errors
The process, uncovered some bugs which I had no idea existed. For example, the newbie trap
my $foo, $bar;
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:
- Variables in subs that genuinely needed to retain values between calls. I learned the trick of using 'my' variables in an outer block to the sub, and learned all about closures in the process.
- Symbolic references. Many of these were rewritten to use data structures. Other instances the code was genuinely doing meta-symbolic generic stuff, so I embedded the critical code in blocks with no strict 'refs';
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.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.