Archive - Originally posted on "The Horse's Mouth" - 2010-06-27 19:57:18 - Graham Ellis
Three part article ... this is part 2. Jump to part [1] or [3]
Perl is the most tremendously powerful and fully featured langauge - you've probably seen that from the first set of sample programs from last week's course, which I wrote up over the following weekend to share with a wider audience.
Here ... is part 2 of 3 of those examples.
P207 File Handling and Formatting
Formatting of numbers - especially when it comes to answers which contain monetary amounts - is something delegates are keen to get to quickly. A decision to raise VAT from 17.5 to 20 % leaves us with some interesting decisions on the pricing of hotel rooms - and it also lead me to a demonstration of formatting.
I have also posted further thoughts, and sample output [here]
P208 Lists - Perl's Arrays
Perl's lists do what other language's arrays do and much more - and I often write short demos during courses that show some of the things you can do. Topics such as sorting and context always come up ... when to use an "@" and when to use a "$" are things that many delegates who've tried (and failed) to learn Perl elsewhere come to us needin help with.
You can do clever - very clever - things with lists as a whole in Perl (contrast some other languages, where you have to write a loop). Those clever things include filtering a list for items that match a certain pattern (grep) and performing the same operation on every member of an incoming list to give an outgoing list (map).
There's a demonstration that selects all of the towns with "ington" in their names and then SHOUTS them at you ... in one of the new demos.
Source code of demo [here]
Data file that goes with it [here]
A further example using the same data file reformats the lines of data - [source].
P209 Subroutines in Perl
I have told the updated story of Romeo and Juliet before - [here] - and how Romeo needed to buy two ladders to get Juliet out for the evening to go down The Tavern, in spite of her dad's objections.
Romeo, being an efficient programmer, used a function to calculate the ladder lengths rather than write the same code twice ... and so this turned in to a "sub" example for my delegates. This is often quite a hard exercise to grasp, as delegates are so concerned towrite code that works that they're not thinking of extensibility.
My sample answer - including a full statement of the question - [here]
Parameters in Perl are passed into subs in the @_ list ... which gives them really horrid names like $_[2] within the sub for the third parameter. So you'll want to take the list of parameters and in most cases copy it into a list of scalars, each of which you'll want to make into a lexically scoped local variable (so it doesn't get mixed up with other variables of the same name). Then you'll want to place your subs in a separate file - that will be a package or namespace that you'll load into all programs that use the same subs with a use statement, or occasionally a require.
I have added three examples - showing each of these steps in turn - onto our site.
Local subroutines using @_ - see [here].
Naming the variables, but keeping the subs in the same file and package - see [here].
And the thirds example - see [here]. It's much shorter, but you'll also need the module that goes with it which is [here]
P210 Topicalization and Special Variables
Command line parameters may be picked up in Perl in the list @ARGV (you can also read from a file named on the command line through the empty file handle <>.
A final example in this section - a "one-liner" which isn't really one line, because the command line options tell Perl to run a loop around the code, and to split each line ... without any actual source at all! See [here]
P211 Hashes
An example written towards the end of a five day Introduction to Programming in Perl course - and it shows the use of lists, hashes, and then loading of a perl module from another file.
Some of the techniques used aren't necessarily best practise as this was a live example, and there are some things shown for clarity rather than efficiency - and clarity for newcomers to Perl code who often find two simpler statements easier than one more complex one. Some variable names were chosen to show flexibility in variable naming, rather than for ease of later maintainance.
Using a hash to keep a note of unique words / IP addresses / string values is much more efficient than maintaining a list of all that you've come across so far and having to keep rescanning the list for matches.
In a hash, keys must be unique, but data value don't need to be so (indeed - the example just above set every value to an identical 1 !)
When you call up a list of the keys of a hash (using the keys function), the keys are returned to you in an order that may APPEAR to be random - but is not; it's all to do with how elements can be accessed quickly and it's called a hashing technique.
If you want to sort a hash, you CANNOT ... but you can sort a list of the keys, and then go through each member of the hash in turn in the order of the sorted keys. It's sort of sorting by the back door ;-). You can even sort the list of keys based on the values they point to in the hash. Clever stuff!