Are you learning Perl? Some more examples for you!
Archive - Originally posted on "The Horse's Mouth" - 2010-06-27 19:54:27 - Graham Ellis
One of the things that you'll get on a Well House Consultants course is new code written by the tutor in front of your very eyes. We do that because:
• you need to learn not only how code works, but also the though code process behind the code - not only how it is written in a ceratyin way but why.
• you need to be able to ask questions as we go along, and not just when presented with a final answer.
• you gain far more from your course if we use your data / tailor the course to your needs as we go along - if we keep it relevant.
There's a downside as far as we're concerned, though - it means that we need to have every single class taught by someone who knows Perl REALLY well - we can't put in a "read the book to the class" guy or gal if we're a bit busy one week. But that downside to us is the most tremendous upside to you - you get someone who REALLY know Perl well, and that's on every course.
Often, the examples aren't especially tidy when we finish the course; they may be emailed to the delegates if requested, or they may be tossed as having fulfilled their purpose. However, I have spent a great deal of this weekend documenting the Perl examples I wrote to a higher standard, so that they're suitable for publication and reuse.
This post is the first of three that will introduce the new examples, subject by subject. Please follow links to the source code where it's of interest, as you'll find further documentation - sometimes line by line where it's appropriate.
And if you would like to learn Perl from us ... please look at our Perl training course options. Or send me an email to discuss your own particular needs.
P201- Perl Introduction
There's a lot to Perl - and early on in learning the language, delegates need some overview of where they're headed - a morale boosing view to say "look - with a few simple elements, you'll be amazed what you can do".
So at the end of the first day of training, I write some fresh examples using what I've taught in that first day, with a few extras along the "we'll see this in the next day orr two" lines.
Here are those examples from last week ...
Counting records of different types from a large log file - [source]
An extended version of that demonstration, which analyses all the possible record types - [source]
And another example - this time with sorted output [source]
And finally, by special request of two of the delegates, output in XML - [source]
P202 - Perl Fundamentals
Before you start coding in ANY language, you should think ahead and plan what you're doing - and a good way to do this is to write your comments first. I did this during the recent Perl course, with programs to calculate body/mass index.
I then went on to add conditionals to the code - not only does the second program tell you your BMI, but it tells you what it means (please consult your doctor and don't trust us - it's just a simple general case on the Perl course and we cannot take responsibility, etc ...)
The final example adds a loop - you enter your height and weight, and it produces a whole table to show what happens to your BMI as you loose and gain weight (there's an assumption that your height won't change!)
Input, calculations, output. Conditionals and loops. Once you've got those basics, you can write some useful programs.
P205 - Initial String Handling in Perl
Does 3 equal 3.0? You may say "yes" and you may say "no" - both answers are sort of correct - the real question is "what to you mean by equals".
Perl has three different ways of comparing - and all are illustrated in a sample program - [here] - which I wrote during the course last week.
To add further complexity, but to make Perl much easier to use once you understand it, Perl 5.10, Perl 5.12 and Perl 6 onwards have an extra operator - the intelligent match operator - which chooses between the three well established comparisons, and others beside, if you use ~~. See [here] for more details.
What's the difference between single and double quotes? What are q and qq? What is a "here document". There's an explantion in the comments - and a sample in the source - [here]
P206 More Perl Loops and Conditionals
Perl has a large number of ways of doing anything - and nowhere will you see that more than with loops and conditionals - a program showing some of the more esoteric examples can be found [here] - an excersize which I asked delegates to do (then did myself) to make use of a wide range of alternatives.
Some of the extra ways - though are VERY useful in use, such as the ? : operator which is a short form of if and else. I wrote an example, and it's documented [here].
Pictures - delegates on last week's course. And it's not ALL study - on the evenings, I took a stroll in the countryside near to us, as I often do. And I was delighted to be accompanied by several delegates and to help promote our motto - "Come as a student, leave as a friend"
To be continued ....
Three part article ... this is part 1. Jump to part [2][3]