Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
Multiway branches in Perl - the given and when syntax

Three way choiceIf you want to perform a multiple alternative branch in Perl, the traditional way has been to use if, a series of elsif (note the spelling!) checks, and a final else if you want to add a default or otherwise condition [example]. Until recent versions, there was no switch statement or equivalent. That's because the "traditional" switch / case construct of C and other languages is somewhat ugly, with break statements often needed an the end of each case, and limitations as to what the cases can actually check for.

But in the Perl 6 spec, and added as a back-implemented feature in the current Perl 5 releases, is the given ... when structure. In this, you establish the variable / expression on which you want to test (you "topicalise it") in the given clause, and you then add a whole series of when clauses, each of which contains *something* to match the topicalised value against. Let's see an example:

given (uc($pc)) {
  when ("") {
    say ("You didn't enter anything"); }
  when (/^[A-Z]{1,2}[0-9][0-9A-Z]?\s+\d[A-Z]{2}$/) {
    say ("That's the format for a British postcode"); }
  when ...


You'll see that once again, the Perl team lead by Larry Wall has come up with something that's very neat and clever and much more practical than a conventional switch. You'll note that there is NOT a break statement added to the baggage of each of the conditional code blocks to prevent it falling through to the next section. And you'll note that my when clause can contain a string or a regular expression. Actually that's just the tip of the iceberg - Perl's when clause is using the new intelligent match operator ~~ which works out what to match depending on the context in which it is used ... and the right half of context is set within the when clause by the the type of data in the brackets. The first one tests:
  ($_ ~~ "")

notes

• If your multiple way branch (code as if - elsif) would have required a final safety net to catch anything that didn't match and of the cases, you would have used an else. With given, the equivalent is a default clause:

default {
  say ("Not one of the regular formats we know");
}


• If - unusually - you do want the code to drop through and continue testing cases, even if you've found one that's true, you can use a continue. Please note that continues to test - it does not drop into the next case and perform that irrespective of whether that next match worked as would happen with C's switch.

• There's a full piece of code showing what I've talked about [here] - matching specific postcodes and particular patterns (and a rare chance to include places like Tristan da Cunha on our web site!).


P.S. - The three way point isn't strictly correct as an illustration ... I should really have used a "ladder" of single points ...
(written 2010-09-22, updated 2010-09-23)

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

P256 - Perl 6 Look Ahead
  [3077] Perl 6 - significantly nearer, and Rakudo looks very good - (2010-12-02)
  [2816] Intelligent Matching in Perl - (2010-06-18)
  [2559] Moving the product forward - ours, and MySQL, Perl, PHP and Python too - (2010-01-01)
  [1721] Perl 6 - When will we have a production release? - (2008-07-26)
  [1417] What software version do we teach? - (2007-10-31)
  [1215] An update on Perl - where is it going? - (2007-06-03)
  [995] Ruby's case - no break - (2006-12-17)
  [582] DWIM and AWWO - (2006-01-30)
  [550] 2006 - Making business a pleasure - (2006-01-01)
  [113] A Parallel for Perl 6 - (2004-11-09)
  [89] When will Perl 6 be available - (2004-10-15)


Back to
Cheap Country Hotel in Melksham, Wiltshire?
Previous and next
or
Horse's mouth home
Forward to
Well House Consultants - a potted history
Some other Articles
Should the public sector compete with businesses? and other deep questions
Perl - doing several things at the same time
What does blessing a variable in Perl mean?
Well House Consultants - a potted history
Multiway branches in Perl - the given and when syntax
Cheap Country Hotel in Melksham, Wiltshire?
Testimonials - Well House Consultants Open Source courses
An introduction to file handling in programs - buffering, standard in and out, and file handles
Removing the new line with chop or chomp in Perl - what is the difference?
Well House Manor - the history of the hotel
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., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/2967_Mul ... yntax.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat