The Linux distillery
Bringing the world of Linux to you, this blog will cut through the tech and show you how it works and how to use it, in terms that apply to any distro. RSS
Australian IT JOBS : Sydney IT jobs, UNIX jobs, Linux jobs, Java jobs, ASP jobs Linux.conf.au Linux.conf.au
Technology news and Jobs arrow The Linux distillery arrow Legends of FOSS: Perl 5.10 is here
Legends of FOSS: Perl 5.10 is here PDF Print E-mail
Written by David M Williams   
Wednesday, 02 January 2008
Switch and smart matching
Perl 5.10 brings forth a switch statement – like C has had for decades, but with – in the Perl way – great lexical enhancements. Three new keywords implement the switch construct, given, when and default.
A sample block of code might be

given ($foo) {
  when (undef} { say ‘$foo is undefined’; }
  when (/^abc/) { $abc = 1; }
  when (/^def/) { $def = 1; }
  when (/^xyz/) { $xyz = 1; }
  when {$_ < 100) { say ‘$foo is a number and is less than 100.’; }
  default { $nothing = 1; }
}

In this sample, the contents of the variable $foo are inspected. Depending on its value, a different result will occur. If $foo is undefined, a message is simply output to the screen. If $foo holds a number and is less than the value 100, a different message is printed. If $foo contains one of the series of letters “abc”, “def” or “xyz” then individual counters are incremented. Failing any of these tests, the default clause kicks in and another variable, $nothing, is incremented.

Note the ($_ < 100) condition; the given keyword will assign its parameter to $_ within the scope of the block.

You’ll also notice the absolute simplicity of the conditions in the when clauses. This is called smart matching and it’s not just available in such switch constructs. You can also use smart matching anywhere else in Perl through a new comparison operator which is represented by two tilde characters in a row, ie ~~.

~~ has tremendous power; You can use it to compare two variables, to find if a regular expression occurs within a string, to find if a value exists in an array and so forth. For example, if you want to test if scalar $needle is in array @haystack you can use the condition

if ($needle ~~ @haystack) ...

CPAN have a table showing the type of match performed by the smart match operator based on the inputs given. Needless to say, both switch and smart matching are tremendous new language features which add great expressive power to the language.

say
Like switch, say is a new language feature and has to be enabled. Its purpose is pretty simple; it outputs a line of text like print() already does – but say also adds a newline character.

state
The third new turn-on-able feature is state variables. This are a whole new class of variables which behave in a similar way to my variables. They are visible only in their lexical scope but their value is persistent across calls – each time you re-enter a scope with state variables, the variables will have their previous values. They will not be undefined each re-entry.

This can help alleviate requirements for global variables and greatly enhance loosely coupled, encapsulated, modules as good programming practice should dictate. For instance, to maintain a private counter which cannot be affected by anything outside its own scope, you could write the following simple code:

use feature ‘state’;
sub gimme_another { state $x; return ++$x; }

Note that state variables can be assigned values – and indeed, they will need to be at some point or else they are undefined like any regular variable. In this case, a state variable declaration combined with an assignment is only processed the very first time. A line like

state $x = 42;

would only be executed the very first time.

Read on for some more cool new stuff!

CONTINUED








 
< Prev   Next >
Contact , Register , Advertise with iTWire , Links , Register , About iTWire , Feedback , Post your jobs , Events , iTWire site map , Start Blogging
Industry Releases , Submit your release now , Start submitting to iTWire , How to post video