Training, Open Source Programming Languages

This is page http://www.wellho.net/mouth/2815_swi ... -Perl.html

Our email: info@wellho.net • Phone: 01144 1225 708225

 
For 2023 (and 2024 ...) - 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))
switch and case, or given and when in Perl

There's a lot of well established Perl code running in many places, using versions of the language that have been used for years. But the language should not stand still, and so extra features get added in at each intermediate release level ... and that then gives us the conundrum of what we should provide as the major focus of our course, and what you should use in code you write.

I'll answer the easy question - "what should YOU use" - first. But I'll answer it with another question - "What versions of Perl will your program be required to run on?". If you can answer, with certainty, that you'll be developing and running on Perl 5.10 - then feel free to use the new features if they provide a significant improvement for you in maintainability / readability, or if they allow you to bring in a module that a third party has written and which requires 5.10. But if there's any chance of Perl being loaded at a pre-5.10 level on even one system that your code will be running on, then you should avoid the new facilities until such time as the older systems are replaced or upgraded.

The more difficult question is "what should we teach?". On one hand, we want to be truly up to date, and on the other hand we must teach people coding techniques that will allow them to maintain the existing code they have (so must major on the better established features) and will work for them in the new code they have to write. Reports such as the Perl survey give us an annual update on what levels of Perl people are using, and at present our public courses contain some examples showing the Perl 5.10 features, but do not subsequently rely on them in following examples, preferring to stick with longer-established features of the language. Delegates who will be using Perl 5.10 (or 5.12!) are welcome to use the new features during practical sessions; delegates who will be writing Perl 5.8 compliant code can, though, do all of the practical sessions without resorting to features that won't be available to them when back at work.


What's the big deal anyway?

As an example, perl has lacked a switch statement since it was written. There have been ways of "kludging" it, but in reality the traditional switch of other languages - with its need to have a break at the end of each case, and often with limitations of what can go in a case - was pretty nasty. Perl 5.10 (in preparation for Perl 6 syntax) has a given and when structure which is a much more modern and nicer (IMHO) implementation of a multiway branch. There's an example [here] on our site.

You'll note that switch and case from other languages are replaced by given and when (to signify different use) in the headline examples. [[You'll also find that good ole eclectic Perl does actually now allow for the "switch" and "case words to be used too :-( ... see [here].]]

When you specify an expression or variable in your given, that value is stored into $_ for the duration of the switch block (the old value is restored afterwards) so that you can give a whole series of when clauses which use the new intelligent match operator (~~) to test for truth. It sounds very much like a switch so far ... but here comes the big change. Once a case has been matched and acted upon, your program will continue to run from the end of the switch block. It does not drop through into the next block, as happens in languages where case is essentially a label. Or - put another way - given and when implement a multiway branch that does away with the need for a break statement to terminate almost every block. You ARE able to explicitly call up a "drop through and try the next tests too" behavior using a continue statement.

Example code snippet:

given ($name) {
  when (/Ellis/i) { print "An Ellis ... "; continue; }
  when ("Graham") { say "exactly Graham"; }
  when (/Graham/) { say "contains Graham"; }
  when (/Graham/i) { say "contains Graham but case is wrong"; }
  default {say "no Graham";}
}


Illustration - Delegates on a recent private Perl course

(written 2010-06-17, updated 2010-06-20)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
P256 - Perl 6 Look Ahead
  [89] When will Perl 6 be available - (2004-10-15)
  [113] A Parallel for Perl 6 - (2004-11-09)
  [550] 2006 - Making business a pleasure - (2006-01-01)
  [582] DWIM and AWWO - (2006-01-30)
  [995] Ruby's case - no break - (2006-12-17)
  [1215] An update on Perl - where is it going? - (2007-06-03)
  [1417] What software version do we teach? - (2007-10-31)
  [1721] Perl 6 - When will we have a production release? - (2008-07-26)
  [2559] Moving the product forward - ours, and MySQL, Perl, PHP and Python too - (2010-01-01)
  [2816] Intelligent Matching in Perl - (2010-06-18)
  [2817] Setting a safety net or fallback value in Perl - (2010-06-19)
  [2967] Multiway branches in Perl - the given and when syntax - (2010-09-22)
  [3077] Perl 6 - significantly nearer, and Rakudo looks very good - (2010-12-02)

P206 - Perl - More Loops and Conditionals
  [138] Perl - redo and last without a loop - (2004-12-02)
  [299] What - no switch or case statement? - (2005-05-03)
  [657] The ternary operator in Python - (2006-03-25)
  [962] Breaking a loop - Ruby and other languages - (2006-12-03)
  [1191] Smart English Output - via PHP and Perl ? : operator - (2007-05-18)
  [1220] for loop - how it works (Perl, PHP, Java, C, etc) - (2007-06-06)
  [1582] Ruby, C, Java and more - getting out of loops - (2008-03-19)
  [1696] Saying NOT in Perl, PHP, Python, Lua ... - (2008-07-04)
  [1825] Question Mark - Colon operator (Perl and PHP) - (2008-10-08)
  [2711] For loop - checked once, or evety time? Ruby v Perl comparison and contrast - (2010-04-07)
  [2824] A pint of Black Rat, and a lazy barman - (2010-06-25)
  [2832] Are you learning Perl? Some more examples for you! - (2010-06-27)
  [2892] Alternative loops and conditionals in Ruby and Perl - (2010-07-28)
  [2972] Some more advanced Perl examples from a recent course - (2010-09-27)
  [3200] How a for loop works Java, Perl and other languages - (2011-03-12)
  [3398] Perl - making best use of the flexibility, but also using good coding standards - (2011-08-19)
  [3619] Ruby v Perl - a comparison example - (2012-02-21)
  [3914] While, for, foreach or something else to loop. - (2012-11-06)


Back to
Python - splitting and joining strings
Previous and next
or
Horse's mouth home
Forward to
Intelligent Matching in Perl
Some other Articles
Some more pictures ...
File open and read in Perl - modernisation
switch and case, or given and when in Perl
Python - splitting and joining strings
Iterating over a Perl list and changing all items
What is Perl?
Igloos melt in the summer, but houses do not
A course review - for the tutor to complete
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).

© WELL HOUSE CONSULTANTS LTD., 2024: 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/2815_swi ... -Perl.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb