Main Content

switch and case, or given and when in Perl

Archive - Originally posted on "The Horse's Mouth" - 2010-06-17 18:13:14 - Graham Ellis

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