Exercises, examples and other material relating to training module P209. This topic is presented on public courses
As your code grows in size, you'll want to divide it down into manageable chunks for ease of maintenance, and so that you can reuse logic at different points in the same application, or even from different applications. Perl's way of naming a block of code is a "subroutine".
|Articles and tips on this subject||updated|
|3833||Learning to use existing classes in Perl|
If you've got a whole lot of logic that you want to be able to run on data of a certain type, you'll typically put all of that logic into a grouping of its own - typically known as a package or a class, and you'll then call that logic at a high level from outside the package or class. This is a great ...
|3574||Perl functions such as chop change their input parameters|
Typically, functions / named blocks of code take a series of operands / parameters as inputs, and return a result which is saved into another variable - here's an example in Perl:
$size = length($persname);
Incoming value - read from $persname
Action defined in - length
Outgoing value - ...
|3066||Separating groups of variables into namespaces|
If your program's going to grow in size beyond a certain point, you'll need to use some sort of namespace or package to segment global names into different groupings - moving from a single name to a forename / surname type system, if you like to think of it that way.
In C++, it's namespaces - see [here] ...
|2929||Passing a variable number of parameters in to a function / method|
How many columns are there in each row in a table? It will vary, depending on the table!
If I want to write a "row" function in PHP, passing in as parameters each of my columns, I don't know the parameter count. But I can find out using the fun_num_args function, and I can then get each of them using ...
|2833||Fresh Perl Teaching Examples - part 2 of 3|
Three part article ... this is part 2. Jump to part  or 
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.
|969||Perl - $_ and @_|
Perl's a great language for special variables - variables that are set up without the programmer having to intervene and providing information ranging from the number of lines read from the current input file ($.) through the current process ID ($$) and the operating system ($^O). Other special variables ...
|2550||Do not copy and paste code - there are much better ways|
If you find yourself using copy and paste to duplicate a piece of code, take a step back and think again. You're doubling your maintenance work, with two separate sets of code to look after and keep in step from that day forwards.
Are you going to give me the "but I have to change one copy a bit" excuse? ...
|2069||Efficient calls to subs in Perl - avoid duplication, gain speed|
When you call a sub in Perl (a subroutine - a named block of code), you pass in a list of parameters in the list called @_. It's easy and straightforward ... but as the length of your list increases, it starts to become less that efficient.
Say - for example - I had a 35 Mbyte log file (and I really ...
|1921||Romeo and Julie|
Romeo wants to take his girlfriend Julie out to The Jolly Judge tomorrow night, but Julie's Dad (who really doesn't approve of young Romeo) has shut her in her room on the first floor of their home, which as you can see is on the top of a substantial mound.
Being a resourceful lad, Romeo is going to ...
|1860||Seven new intermediate Perl examples|
From the "learning to program in Perl" course I'm running this week, I am pleased to present seven new examples ... written during the course, in front of the delegates, to show them NOT ONLY how the code works, BUT ALSO how a programmer will develop such code.
Lists and Context
* If you use an @abc, ...
|1850||Daisy the Cow and a Pint of Ginger Beer|
One of the trickiest aspects of learning for newcomers to programming is to know when to split out parts of their code into named blocks. Newcomers can easily see that it's a good idea - as it means that code can be tested in smaller chunks, and that code can be shared (reused) - but it's not always ...
|1826||Perl - Subs, Chop v Chomp, => v ,|
During courses, I end up writing a lot of short demonstrations to show particular features of a language - this week, it's a Perl Programming Course so those examples are in Perl.
Some interesting Perl facts ...
a) The => operator can be used to replace the , ("the equals, greater than can be used ...
|1782||Calling procs in Tcl and how it compares to Perl|
In languages such as Java, you must call your named blocks of code (methods) with the correct number and type of parameters, but in Perl you may call them with as many or as few parameters as you like ... then write the named block of code (a sub) to handle (or ignore) whatever it gets.
This week, I'm ...
|1784||Global - Tcl, PHP, Python|
PHP, Python, Tcl and a number of other languages have a global keyword. And it's a misnomer in most (if not all) cases.
To the un-initiated, "Global" means "worldwide" or "shared all around". So you would think that if you declare something as global, you're going to be sharing it with the same thing ...
One of the vital topics on all our programming courses is that of variable scope. Variable Scope may be defined as the area of a program in which a variable is visible, and how long that variable is accessible for.
Why do I describe variable scope as a vital subject when you can write simple programs ...
|1202||Returning multiple values from a function (Perl, PHP, Python)|
Function in PHP and Python and subs in Perl can only return one "thing" - one item, one object. But all three languages allow that returned item to be a collection - in other words, a composite. And all three languages provide a very easy way of breaking the returned structure down into a series of individual ...
|1163||A better alternative to cutting and pasting code|
If you're new to coding, you'll be so concerned to be writing code that works that you may not take a look at coding technique. Your nose will be so close to the grindstone as you work that you won't take the time to look and ask "Do I need to keep grinding anyway?"
If you find yourself writing a piece ...
|775||Do not duplicate your code|
If you've writing or maintaining a program and you find yourself cutting and pasting a chunk of code, STOP and think again.
By duplicating a block of code, you're duplicating your maintainance task from that point onwards - any fixes applied to the original much be applied to the copy too. And that's ...
|531||Packages in packages in Perl|
In perl, all your variables (except "my" ones, which we'll leave for another day) are arranged into packages, and by default that's a package called "main". So if you write about a scalar called $jeremy, that's really $main::jeremy and if you write about a named block of code called &mary, that's ...
|588||Changing @INC - where Perl loads its modules|
Where does Perl load modules from in its use and require statements? It loads them from directorys in a special list called @INC, from files with a .pm extension in those directorys.
When Perl's installed, @INC is set to a list of directorys that includes generic locations for its standard modules, ...
|bandq.pm|| package with "romeo" example|
|bridges.pm|| subroutines called up from example applications|
|control|| Example of how you should NOT repeat code|
|courses.pl|| Sample (test) program to try out a diary|
|cov2.pm|| Coverage as a class. Allows multiple parallel coverage sets!|
|coverage.pm|| Routines to combine integer ranges to provide overall coverage information|
|covertest|| Sample application - coverage test given ranges of integers|
|ct2|| Coverage test - application using coverage objects|
|diaryev.pm|| Sample class for courses.pl demo|
|email.pm|| email class of objects - a very simple class|
|funcall|| passing a hash to a sub|
|future|| Using a class in Perl|
|globvar|| demonstration that variables default to global|
|l2|| lists / context demo|
|mailfilter.pm|| package to encapsulate information - NOT really objects.|
|mailister2|| including package name in package methods|
|maillister|| using a module to retain information|
|mc|| Email analysis via a package|
|mc2|| using email objects|
|melkservice|| First use of training class|
|mf2.pm|| package of subroutines for mailfilter2|
|ms2|| calling subroutines in a different file|
|mysub|| calling a subroutine in the same file|
|myvar|| Use of my to reduce variable persistance and scope|
|romeo|| Romeo and Julie - solution|
|romeo2|| Answer to Romeo and Juliet question|
|scope|| Scope - our, my and local|
|splitty|| using subs to avoid repitition|
|standardstuff.pm|| Standard and repeated code - in own file|
|subby|| repeated code in a sub|
|subgetnum|| Sample use of subroutines - returned parameters|
|subgnrange|| Passing parameters in and out of subroutines|
|substamp|| Sample use of subroutines|
|tip1|| Parameters in and out of a sub|
|tip2|| passing data in and out of subs|
|tipsy|| Calling in a module from you Perl program|
|train.pm|| Class for training - first use|
|ua.pm|| Identify whether a web access record is from a spider|
|utils.pm|| Standard functions - tipsy demo|
|valread.pm|| standard subroutines to include in several programs|
Some modules are available for download
as a sample of our material or under an Open Training Notes License
for free download from [here]
The what and why of subroutines.
Structured and object oriented programming.
Calling a subroutine.
Loading a subroutine from another file.
Local, my and package variables.
Loading a class and using objects.
If you are looking for a complete course and not just a information on a single subject, visit our Listing and schedule
Well House Consultants specialise in training courses in
. We run
throughout the UK (and beyond for longer courses), and
at our training centre in Melksham, Wiltshire, England.
It's surprisingly cost effective to come on our public courses -
even if you live in a different
country or continent to us
We have a technical library of over 700 books on the subjects on which we teach.
These books are available for reference at our training centre.