Three part article ... this is part 3. Jump back to part  
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, $1, $2 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.
Sample source - [here]
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   (written 2010-06-27)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articlesP212 - Perl - More on Character Strings 
Binary data handling - Python and Perl - (2015-03-09) 
First match or all matches? Perl Regular Expressions - (2012-11-19) 
Converting codons via Amino Acids to Proteins in Perl - (2012-04-25) 
Possessive Regular Expression Matching - Perl, Objective C and some other languages - (2012-03-12) 
Serialsing and unserialising data for storage and transfer in Perl - (2012-02-28) 
The difference between dot (a.k.a. full stop, period) and comma in Perl - (2011-12-09) 
Single and double quotes strings in Perl - what is the difference? - (2011-08-30) 
DNA to Amino Acid - a sample Perl script - (2011-06-24) 
How much has Perl (and other languages) changed? - (2011-06-10) 
Looking ahead and behind in Regular Expressions - double matching - (2010-12-23) 
Object Orientation in an hour and other Perl Lectures - (2010-11-18) 
Arrays v Lists - what is the difference, why use one or the other - (2010-10-10) 
Further more advanced Perl examples - (2010-07-19) 
Unpacking a Perl string into a list - (2010-07-16) 
Binary data handling with unpack in Perl - (2010-06-10) 
Want to do a big batch edit? Nothing beats Perl! - (2010-03-01) 
Making variables persistant, pretending a database is a variable and other Perl tricks - (2009-08-27) 
Running a piece of code is like drinking a pint of beer - (2009-06-11) 
Perl substitute - the e modifier - (2008-12-16) 
Finding words and work boundaries (MySQL, Perl, PHP) - (2008-08-03) 
Equality and looks like tests - Perl - (2008-07-29) 
Handling Binary data (.gif file example) in Perl - (2008-01-17) 
Ignore case in Regular Expression - (2007-09-08) 
Regular expressions made easy - building from components - (2007-08-16) 
Substitute operator / modifiers in Perl - (2007-06-28) 
Commenting a Perl Regular Expression - (2007-06-12) 
Perl, the substitute operator s - (2007-06-08) 
Matching within multiline strings, and ignoring case in regular expressions - (2006-11-25) 
C++ and Perl - why did they do it THAT way? - (2006-11-16) 
Coloured text in a terminal from Perl - (2006-05-29) 
Don't expose your regular expressions - (2006-02-15) 
Storing a regular expression in a perl variable - (2006-02-09) 
Perl Regular Expressions - finding the position and length of the match - (2006-02-02) 
Remember to process blank lines - (2006-01-31) 
Commenting Perl regular expressions - (2005-09-30)P213 - Perl - Creating your own Classes 
Learning to use existing classes in Perl - (2012-08-10) 
Learning Object Orientation in Perl through bananas and perhaps Moose - (2010-12-21) 
What does blessing a variable in Perl mean? - (2010-09-24) 
When should I use OO techniques? - (2009-05-11) 
Introduction to Object Oriented Programming - (2008-12-06) 
Object Oriented Perl - First Steps - (2008-11-01) 
Example of OO in Perl - (2008-06-03) 
Object Oriented Programming in Perl - Course - (2007-11-18) 
Perl for Larger Projects - Object Oriented Perl - (2007-08-25) 
Blessing in Perl / Member variable in Ruby - (2006-12-14) 
Answering ALL the delegate's Perl questions - (2006-12-09) 
When to bless a Perl variable - (2005-03-15) 
Bellringing and Programming and Objects and Perl - (2005-02-25)P258 - Perl - Solaris, OSX and Linux 
Pre and post increment - the ++ operator - (2009-02-03) 
Running operating system commands in Perl - (2008-07-08) 
Changing a screen saver from a web page (PHP, Perl, OSX) - (2008-05-06)P403 - Perl - The Common Gateway Interface 
Perl and CGI - simple form, and monitoring script. - (2011-09-17) 
Perl and the Common Gateway Interface - out of fashion but still very useful? - (2009-12-26) 
Handling nasty characters - Perl, PHP, Python, Tcl, Lua - (2009-06-14) 
CGI v mod_perl - (2004-09-11)P667 - Perl - Handling Huge Data 
How to interact with a Perl program while it is processing data - (2011-07-31) 
Speeding up your Perl code - (2011-07-30) 
Macho matching - do not do it! - (2010-06-13) 
How are you getting on? - (2010-06-13) 
Long job - progress bar techniques (Perl) - (2009-08-26) 
Preventing ^C stopping / killing a program - Perl - (2008-12-05) 
Progress Bar Techniques - Perl - (2008-12-03) 
Perl - progress bar, supressing ^C and coping with huge data flows - (2007-10-20) 
Huge data files - what happened earlier? - (2006-06-15) 
Progress bars and other dynamic reports - (2006-03-09)Q904 - Object Orientation and General technical topics - Analysing a Programming Task 
Flowchart to program - learning to program with Well House - (2012-10-14) 
From flowchart to program - code design for the newcomer - (2011-09-29) 
Specification, Design, Implementation, Testing and Documentation - stages of a (Java) programming project - (2011-07-21) 
Perl from basics - (2011-06-20) 
Uploading an image, document or pdf via a browser (php) - (2010-04-10) 
Planning! - (2009-08-08) 
Well structured coding in Perl - (2008-10-24) 
Daisy the Cow and a Pint of Ginger Beer - (2008-10-21) 
Learning to program in Perl - (2008-04-11) 
Perl, PHP or Python? No - Perl AND PHP AND Python! - (2008-01-20) 
Perl and Shell coding standards / costs of an IT project - (2007-09-11) 
The Fag Packet Design Methodology - (2006-06-06)
Some other Articles
Respecting our customers anonimityLorry Parking in MelkshamPerl - the duplicate key problem explained, and solutions offeredA course is more than just a chap giving a lectureTeaching examples in Perl - third and final partFresh Perl Teaching Examples - part 2 of 3Are you learning Perl? Some more examples for you!Recording (a macro) in viPoulshot Village FeteDauncey Gardens, Melksham