The Linux distillery
Bringing the world of Linux to you, David cuts through the tech and shows you how it works and how to use it, in terms that apply to any distro. RSS
IT NEWS      Sustainability        - Virtualisation   
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 E-mail
User Rating: / 13
PoorBest 
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








 
< Next story in category   Previous story in the category >
iTWire Technology feature

Virtualisation

Servermaximise your infrastructure, maximise your business

Read more...

Custom Search
 
You don't need to login to post a comment





Lost Password?
No account yet? Register
Subscribe to our free daily newsletter.
BCI Training – Understanding BCM Principles and Good Practice
August 23, 2008 (All Day)
BCI Training – Understanding BCM Principles and Good Practice consists of 5 one day training modu...

BCI Training – Understanding BCM Principles and Good Practice
August 24, 2008 (All Day)
BCI Training – Understanding BCM Principles and Good Practice consists of 5 one day training modu...

BCI Training – Understanding BCM Principles and Good Practice
August 25, 2008 (All Day)
BCI Training – Understanding BCM Principles and Good Practice consists of 5 one day training modu...

BCI Training – Understanding BCM Principles and Good Practice
August 25, 2008 (All Day)
BCI Training – Understanding BCM Principles and Good Practice consists of 5 one day training modu...

HP QuickTest Professional Public Training
August 25 (9:00 am) - August 26 (11:59 pm), 2008
This instructor-led course provides a comprehensive understanding of using QuickTest Professional...

NICTA Big Picture Seminar - Dr Rodney Brooks "Robotics: Shaped by and Shaping the World in 2000 - 2050"
August 25, 2008 (4:00 pm - 5:00 pm)
ABSTRACT: As the 21st century has dawned we have seen a dramatic uptake of robots for unstructure...

BCI Training – Understanding BCM Principles and Good Practice
August 26, 2008 (All Day)
BCI Training – Understanding BCM Principles and Good Practice consists of 5 one day training modu...

BCI Training – Understanding BCM Principles and Good Practice
August 26, 2008 (All Day)
BCI Training – Understanding BCM Principles and Good Practice consists of 5 one day training modu...

PR Online
August 26, 2008 (9:30 am - 12:00 pm)
PR Online Sydney, August 26, 2008 Speakers: - Michael Henderson, Specrum PR - Jeremy Mitche...

ICT SMART: OFFICE SHOW
August 26 (10:00 am) - August 28 (11:59 pm), 2008
ICT SMART office show, is a business-to-business trade event featuring the latest in business tec...
New event listings
SolidWorks Innovation Day (Melbourne and Adelaide)
October 17, 2008 (All Day)
Hosted by Intercad, SolidWorks’ Innovation Days will give designers, engineers and manufacturers ...

SolidWorks Innovation Day (Sydney)
October 16, 2008 (All Day)
Hosted by Intercad, SolidWorks’ Innovation Days will give designers, engineers and manufacturers ...

SolidWorks Innovation Day (Brisbane and Perth)
October 15, 2008 (All Day)
Hosted by Intercad, SolidWorks’ Innovation Days will give designers, engineers and manufacturers ...

LIXI Industry Forum 2008
September 10, 2008 (All Day)
Wednesday, 10 September 2008 The Westin Sydney The second annual major industry event for the...

BCI Training – Understanding BCM Principles and Good Practice
August 22, 2008 (All Day)
BCI Training – Understanding BCM Principles and Good Practice consists of 5 one day training modu...

BCI Training – Understanding BCM Principles and Good Practice
August 21, 2008 (All Day)
BCI Training – Understanding BCM Principles and Good Practice consists of 5 one day training modu...

View Full Calendar
Subscribe to our free daily e-newsletter
Contact , Register , Advertise with iTWire , Links , About iTWire , Feedback , Post your jobs , Events , iTWire site map , Start Blogging , MyBlogLog page
Industry Releases , Submit your release now