Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
For 2023 - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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, you mean the WHOLE of a list
* If you use $abc[10], you mean a scalar member of a list
* If you use $#abc, you mean the top index number of a list

* If you refer to a list IN A LIST CONTEXT you mean all elements
* In a SCALAR context, you mean the NUMBER OF ELEMENTS in the list
* In a DOUBLE QUOTE context, you mean a string of all element ...
... linked with a space between each of them

See source code at A demonstration of lists and context

Handling command line options with getopts

The getopts sub (in the Getopt::std module that's supplied with the Perl distribution) lets you process command line parameters (from @ARGV) ... filtering out options into flag variables without the need for you to write (and maintain) that code yourself.

See source code at getopts - command line handling in Perl

UK Postcode matching in Perl

This is a testbed program to validate UK postcodes and also to extract the various key elements into other variables.

The section of a postcode up to the mandatory space is known as the "in" as it's inbound to a sorting office, and the section after the space is known as the "out" as it's used to sort at the sorting office into individual post staff delivery runs.

I have used the \b boundary condition in this code - I did NOT want to anchor to start or end of line, but neither did I want to match postcodes that start within a word. \b (word boundary match) is a happy compromise.

See source code at Regular Expression - match and extract from a UK postcode

Running another process on a pipe

If you use backquotes, a command is run on your computer as a separate process and WHEN IT IS COMPLETED, you'll get the results back. If you want to read from the other process as it runs, you can open a piped process instead - that's using open with a pipe character at the end of what would normally be the file name, but is now the command to run your extra process in turn.

See source code at piping a ping into your perl

Splitting Money many ways

Scenario - You have 21 nieces and nephews, and you're going to spend a certain amount of money on them for presents - let's say a total of 1000 pounds. But some of the nieces and nephews may not be in favour (for example, you were not thanked for last year's gift) so you'll split their share amongst the others.

Produce a table, nicely formatted, showing how much money you'll be spending on each child depending on how many are in favour.

See source code at Formatting money and other text in Perl

Single, Double and backquoted strings

In Perl, you can write ...
* A literal string in single quotes
* A string in which \ $ and @ are expanded in double quotes
* An operaring systems command to be run in backquotes
and in each case, the result will be returned to you as the result of the operation if you want to save it into a variable.

If you really want a double quote character within a double quoted string, you can always add an extra \ in front of it - but that facility is NOT similarly available to you if you want a single quote in a single quoted string. So you have to use the alternatives ...
* qq followed by any non-alphanumeric delimter of your choice gives you a double quoted string
* q followed by any non-alphanumeric delimter of your choice gives you a single quoted string
* qx followed by any non-alphanumeric delimter of your choice gives you a shell executed command

Finally, you can write multiline strings in what are know as "here" documents, where the delimiter is a complete string and must occur further down on a line ON ITS OWN (no ;, no spaces) to indicate the end of the block.

See source code at single double and backquotes in Perl

Tiny code in Perl

The code in this example uses the -n option which implies that it runs within a loop that reads each line in turn from a file named on the command line and stores it in the "default input and pattern matching space" a.k.a. $_ ...

It's open to debate whether or not coding in this way is good practise - my personal view is that it's great for occasional utilities where you just need to quickly filter a data file on one or two occasions, but it's not a good idea for long term coding where you need to have maintenance in mind.

See source code (but it's very short indeed!) at using an implicit loop with -n

In all these new examples, I have added an __END__ to the end of my source code to stop the Perl interpretter at that point, and then added in some sample output so that you can see what the code produces when it runs. There are plenty of other resources / samples accessible from the sample source pages too.

We have a public Using Perl on the Web and a public Perl for Larger Projects course both coming up just before Christmas [2008], with space available. Both courses will run again in 2009 ... please follow the links in this paragraph for the next dates if you've missed Christmas!
(written 2008-10-30, updated 2008-11-02)

Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Q803 - Object Orientation and General technical topics - Regular Expressions - Extra Elements
  [943] Matching within multiline strings, and ignoring case in regular expressions - (2006-11-25)
  [1336] Ignore case in Regular Expression - (2007-09-08)
  [1372] A taster PHP expression ... - (2007-09-30)
  [1601] Replacing the last comma with an and - (2008-04-04)
  [1613] Regular expression for 6 digits OR 25 digits - (2008-04-16)
  [1735] Finding words and work boundaries (MySQL, Perl, PHP) - (2008-08-03)
  [2909] Be gentle rather than macho ... regular expression techniques - (2010-08-08)
  [3089] Python regular expressions - repeating, splitting, lookahead and lookbehind - (2010-12-17)
  [3100] Looking ahead and behind in Regular Expressions - double matching - (2010-12-23)
  [3516] Regular Expression modifiers in PHP - summary table - (2011-11-12)
  [3650] Possessive Regular Expression Matching - Perl, Objective C and some other languages - (2012-03-12)

P210 - Perl - Topicalization and Special Variables
  [493] Running a Perl script within a PHP page - (2005-11-12)
  [639] Progress bars and other dynamic reports - (2006-03-09)
  [969] Perl - $_ and @_ - (2006-12-07)
  [1136] Buffering output - why it is done and issues raised in Tcl, Perl, Python and PHP - (2007-04-06)
  [1221] Bathtubs and pecking birds - (2007-06-07)
  [1232] Bathtub example - (2007-06-14)
  [1289] Pure Perl - (2007-08-03)
  [1444] Using English can slow you right down! - (2007-11-25)
  [1508] How not to write Perl? - (2008-01-15)
  [1704] Finding operating system settings in Perl - (2008-07-10)
  [1705] Environment variables in Perl / use Env - (2008-07-11)
  [1728] A short Perl example - (2008-07-30)
  [1829] Dont bother to write a Perl program - (2008-10-10)
  [1922] Flurinci knows Raby Lae PHP and Jeve - (2008-12-04)
  [2833] Fresh Perl Teaching Examples - part 2 of 3 - (2010-06-27)
  [2876] Different perl examples - some corners I rarely explore - (2010-07-18)
  [2972] Some more advanced Perl examples from a recent course - (2010-09-27)
  [3449] Apache Internal Dummy Connection - what is it and what should I do with it? - (2011-09-19)
  [4301] Perl - still a very effective language indeed for extracting and reporting - (2014-09-20)
  [4395] Preparing data through a little bit of Perl - (2015-01-15)
  [4682] One line scripts - Awk, Perl and Ruby - (2016-05-20)
  [4700] Obfurscated code - it might work, but is it maintainable? - (2016-07-02)

P209 - Subroutines in Perl
  [96] Variable Scope - (2004-10-22)
  [308] Call by name v call by value - (2005-05-11)
  [357] Where do Perl modules load from - (2005-06-24)
  [531] Packages in packages in Perl - (2005-12-16)
  [588] Changing @INC - where Perl loads its modules - (2006-02-02)
  [775] Do not duplicate your code - (2006-06-23)
  [1163] A better alternative to cutting and pasting code - (2007-04-26)
  [1202] Returning multiple values from a function (Perl, PHP, Python) - (2007-05-24)
  [1782] Calling procs in Tcl and how it compares to Perl - (2008-09-02)
  [1784] Global - Tcl, PHP, Python - (2008-09-03)
  [1826] Perl - Subs, Chop v Chomp, => v , - (2008-10-08)
  [1850] Daisy the Cow and a Pint of Ginger Beer - (2008-10-21)
  [1921] Romeo and Julie - (2008-12-04)
  [2069] Efficient calls to subs in Perl - avoid duplication, gain speed - (2009-03-07)
  [2550] Do not copy and paste code - there are much better ways - (2009-12-26)
  [2929] Passing a variable number of parameters in to a function / method - (2010-08-20)
  [3066] Separating groups of variables into namespaces - (2010-11-24)
  [3574] Perl functions such as chop change their input parameters - (2012-01-10)
  [3833] Learning to use existing classes in Perl - (2012-08-10)

P207 - Perl - File Handling
  [12] How many people in a room? - (2004-08-12)
  [114] Relative or absolute milkman - (2004-11-10)
  [255] STDIN, STDOUT, STDERR and DATA - Perl file handles - (2005-03-23)
  [616] printf - a flawed but useful function - (2006-02-22)
  [618] Perl - its up to YOU to check your file opened - (2006-02-23)
  [702] Iterators - expressions tha change each time you call them - (2006-04-27)
  [867] Being sure to be positive in Perl - (2006-09-15)
  [1312] Some one line Perl tips and techniques - (2007-08-21)
  [1416] Good, steady, simple example - Perl file handling - (2007-10-30)
  [1442] Reading a file multiple times - file pointers - (2007-11-23)
  [1467] stdout v stderr (Tcl, Perl, Shell) - (2007-12-10)
  [1709] There is more that one way - Perl - (2008-07-14)
  [1841] Formatting with a leading + / Lua and Perl - (2008-10-15)
  [1861] Reactive (dynamic) formatting in Perl - (2008-10-31)
  [2233] Transforming data in Perl using lists of lists and hashes of hashes - (2009-06-12)
  [2405] But I am reading from a file - no need to prompt (Perl) - (2009-09-14)
  [2818] File open and read in Perl - modernisation - (2010-06-19)
  [2821] Chancellor George Osborne inspires Perl Program - (2010-06-22)
  [3326] Finding your big files in Perl - design considerations beyond the course environment - (2011-06-14)
  [3548] Dark mornings, dog update, and Python and Lua courses before Christmas - (2011-12-10)
  [3830] Traversing a directory in Perl - (2012-08-08)
  [3839] Spraying data from one incoming to series of outgoing files in Perl - (2012-08-15)

P205 - Perl - Initial String Handling
  [31] Here documents - (2004-08-28)
  [254] x operator in Perl - (2005-03-22)
  [324] The backtick operator in Python and Perl - (2005-05-25)
  [970] String duplication - x in Perl, * in Python and Ruby - (2006-12-07)
  [987] Ruby v Perl - interpollating variables - (2006-12-15)
  [1195] Regular Express Primer - (2007-05-20)
  [1608] Underlining in Perl and Python - the x and * operator in use - (2008-04-12)
  [1849] String matching in Perl with Regular Expressions - (2008-10-20)
  [2798] Perl - skip the classics and use regular expressions - (2010-06-08)
  [2816] Intelligent Matching in Perl - (2010-06-18)
  [2832] Are you learning Perl? Some more examples for you! - (2010-06-27)
  [2963] Removing the new line with chop or chomp in Perl - what is the difference? - (2010-09-21)
  [3005] Lots of ways of doing it in Perl - printing out answers - (2010-10-19)
  [3411] Single and double quotes strings in Perl - what is the difference? - (2011-08-30)
  [3547] Using Perl to generate multiple reports from a HUGE file, efficiently - (2011-12-09)
  [3770] Sample answers to training course exercises - available on our web site - (2012-06-21)

Back to
Wiltshire at dawn - the tourist trail
Previous and next
Horse's mouth home
Forward to
Reactive (dynamic) formatting in Perl
Some other Articles
Object Oriented Perl - First Steps
About dieing and exiting in Perl
Remember your units
Seven new intermediate Perl examples
Wiltshire at dawn - the tourist trail
Camera with night vision, youth with no vision
November and December Public Course Schedule
A few of my favourite things
Volunteer v Employee - a skewed balance? (FSB)
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page

This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

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

PAGE: http://www.wellho.net/mouth/1860_Sev ... mples.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb