Archive - Originally posted on "The Horse's Mouth" - 2010-06-27 20:15:00 - Graham Ellis
Three part article ... this is part 3. Jump back to part [1][2]
Following on from two earlier posts, here is the final third of the new examples that I wrote during last week's Perl course, and to which I have added extra documentation over the last couple of days.
P212 More on Character Strings
"Does this line contain a possible email address" has a simple answer - yes, or no.
But if the answer is "yes", then there are other questions that arise - corollaries - to which you would like an answer. It's the same in real life ...
"What's the effect of drinking a pint of Black Rat?" The obvious answer is that you get a bit tipsy (or very tipsy if you'e a lightweight). But there are other answers too - the landlord is £2.80 (or however much) richer. There's a glass to was up. You need to visit the loo ...
How can you get at these other answers? In Perl, they're saved into special variables such as $& for the bit of the string that matched your pattern, , and so on for the parts of that which match your capture parenthesis - if any. And so on.
I wrote a sample program that shows this in Perl - [source].
There's also a use of these special variable highlighted in a post code hunter [here]
The substitute operator lets you match a regular expression (one or more times) in an incoming string, and replace the matched text with another string, or with the result of evaluating an expression. The output string may depend on (take some of its values from) the incoming string, based on capture parenthesis.
It's much easier to see an example - there's commented source code [here].
P213 Creating your own Perl classes and modules
What does object orientation bring you? It brings the ability to separate out the code that's associated with a type of data from the calling application, and the ability to define a whole series of data types based on a parent "class".
In Perl, it's implemented through packages and modules, and during the recent course I wrote two files which I'll share here.
Without classes - a piece of "control" code using a package - [source]
With classes - the same example, but updated to use objects - [source]
P258 Perl - operating system specific. Unix, Solaris, OS X and Linux
Perl is operating system independent - or largely so. If you code along the lines of "run this operating system command", though, it will only work for you on machines that support that command.
I have a fresh example - [source code here] which runs a df -k command on Unix / Linux in a variety of ways ...
P403 The Common Gateway Interface to Perl
In my early days of teaching Perl, many of our customers were using the Common Gateway Interface (CGI) to hook perl scripts up to web URLs. And very good that was too. But these day, PHP, Ruby on Rail, Django, Java's JSPs and Servlets, and Perl's own Mason - amongst others - have come along, and their specific systems that make the web work so much more integrated. Does CGI still have a place, then?
Yes - it has some niches, and one of last week's delegates has one. A whole load of Perl coe, used for data munging and systems admin, and the occasional need to feed some of it back onto the web.
CGI is designed to take a STDIN -> STDOUT Perl program and with a bit of fiddling get it runing on the web, so it should be no surprise to you than our BMI example from last week is available online too ;-)
Source code [here]
And run it [here] ... it's not particularly well laid out on the web - but I have added a couple of security thinsg to ensure that the well know holes are closed.
P667 Handling Huge Data
There are extra considerations you'll want to make when you're handling huge amounts of data - such as efficient ways to handle it in bulk, and ways to "sample" records so that you can run stats based on a manageable subset. We cover many of these techniques on our Perl for Larger Projects course. However, questions came up last week and a short new example, showing elements of what we cover on the later course, was written.
Source code [here] - but please note that is is NOT optimised yet (it's shockingly slow!).
Q904 Analysing a Programming Task
There are some examples that come up during the course that don't logically fit under any particular language element, and here's one of the - so I've indexed it under techniques.
If you want to get several reports from a data flow, you could save that parse tha data multiple times. Yuk! It's better to store the results into a series of variables (use strings that you contatonate, hashes, lists as you feel appropriate) then output each of the complete strings once you've passed through all the data.
For planning a simple application, you need to decide what's to be done once (outside loops) and what needs to be repeated (inside loops). And this requires quite some thought for newcomers to programming.
I wrote an example that used the through process, and over this weekend, after the course, I've added further comments to it. Please take a look [here].
Three part article ... this is part 3. Jump back to part [1][2]