Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact

Well House Consultants
You are on the site of Well House Consultants who provide Open Source Training Courses and business hotel accommodation. You are welcome to browse and use our resources subject to our copyright statement and to add in links from your pages to ours.
Other subject areas - resources
Java Resources
Well House Manor Resources
Perl Resources
Python Resources
PHP Resources
Object Orientation and General topics
MySQL Resources
Linux / LAMP / Tomcat Resources
Well House Consultants Resources
Extras Resources
C and C++ Resources
Ruby Resources
Tcl/Tk Resources
Web and Intranet Resources
Perl module P209
Subroutines in Perl
Exercises, examples and other material relating to training module P209. This topic is presented on public courses Perl Programming, Perl bootcamp

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 subjectupdated
3833Learning 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 ...
2012-08-18
 
3574Perl 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 - ...
2012-01-14
 
3066Separating 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] ...
2010-12-04
 
2929Passing 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 ...
2010-08-20
 
2833Fresh Perl Teaching Examples - part 2 of 3
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 ...
2010-06-27
(longer)
969Perl - $_ 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 ...
2010-06-22
 
2550Do 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? ...
2010-01-06
 
2069Efficient 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 ...
2009-03-07
 
1921Romeo 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 ...
2008-12-03
 
1860Seven 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, ...
2008-11-02
(longer)
1850Daisy 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 ...
2008-10-21
 
1826Perl - 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 ...
2008-10-08
 
1782Calling 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 ...
2008-09-04
 
1784Global - 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 ...
2008-09-04
 
96Variable Scope
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 ...
2008-05-11
 
1202Returning 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 ...
2007-05-24
 
1163A 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 ...
2007-04-26
 
775Do 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 ...
2006-06-30
 
531Packages 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 ...
2006-06-09
 
588Changing @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, ...
2006-06-05
 
Examples from our training material
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
Pictures
During a practical
Our course have plenty of lectures and plenty of practicals
A Perl practical on ladder lengths
Background information
Some modules are available for download as a sample of our material or under an Open Training Notes License for free download from http://www.training-notes.co.uk.
Topics covered in this module
What are subroutines and why?.
The limitations of "single block code".
First use of subroutines.
Structured programming.
Object oriented programming.
Calling a subroutine.
Passing values out of subroutines.
Passing values in to subroutines.
Writing your own subroutine.
Passing parameters in.
Returning values.
Writing subroutines in a separate file.
Scope.
Global Scope.
my variables.
A test program:.
packages.
Calling objects.
Writing a class - an introduction.
Complete learning
If you are looking for a complete course and not just a information on a single subject, visit our Listing and schedule page.

Well House Consultants specialise in training courses in Python, Perl, PHP, and MySQL. We run Private Courses throughout the UK (and beyond for longer courses), and Public Courses 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. Also available is the Opentalk Forum for discussion of technical questions.


You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/resources/P209.html • PAGE BUILT: Wed Mar 6 10:39:20 2013 • BUILD SYSTEM: wizard