
If 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)
26e2
Associated topics are indexed under
P256 - Perl 6 Look Ahead [3077] Perl 6 - significantly nearer, and Rakudo looks very good - (2010-12-02)
[2817] Setting a safety net or fallback value in Perl - (2010-06-19)
[2816] Intelligent Matching in Perl - (2010-06-18)
[2815] switch and case, or given and when in Perl - (2010-06-17)
[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)
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)
[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)
Some other Articles
Should the public sector compete with businesses? and other deep questionsPerl - doing several things at the same timeWhat does blessing a variable in Perl mean?Well House Consultants - a potted historyMultiway branches in Perl - the given and when syntaxCheap Country Hotel in Melksham, Wiltshire?Testimonials - Well House Consultants Open Source coursesAn introduction to file handling in programs - buffering, standard in and out, and file handlesRemoving the new line with chop or chomp in Perl - what is the difference?Well House Manor - the history of the hotel