Archive - Originally posted on "The Horse's Mouth" - 2008-04-11 13:43:00 - Graham Ellis
For the last few days, I've been teaching Learning to Program in Perl. Unlike a conventional course style, for this one I had been asked to assume no prior programming knowledge, and covered first principles such as variables, conditionals, and loops.
Here is one of the examples I wrote during the course ...
# The bit you do at the start
print "Please enter first number ";
$entry = <STDIN>;
$sofar = 0;
$howmany = 0;
# The bit you keep repeating
while ($entry > 0) {
$sofar += $entry;
$howmany++;
print "Please enter next number (0 to end) ";
$entry = ;
}
# The bit you do at the end
print "The total was $sofar in $howmany entries\n";
You'll note how I have divided a straightforward little application into "the bit that's done once, first", "the bit that's repeated", and "the bit that's done at the end. And you'll note too how important the comments are!
Given a couple of days, we moved on from examples like the one above to a program that was reading and analysing a log file ... and coming out with results. Perl is, after all, the Practical Extraction and Reporting Language