Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl now supports switch statements!

Status
Not open for further replies.

KevinADC

Technical User
Jan 21, 2005
5,070
0
0
US
Perl 5.10.0 introduced a new pragma, "features". One of the features is "switch":

Code:
use features 'switch';

There are some new keywords to use with switch statements: given, when and default.

Read about this new feature and other changes to perl in the History/Changes pages on perldoc:


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I'm not sure I've ever had much use for switch statements. Those are more useful in complicated gui's, and even then dispatch tables are often more preferred. We'll see! Off to go read all about it.

Thanks for the heads up,
- Miller
 
This is how I used to do switch statements:

Code:
my $color = 'red';

for ($color) {
   /blue/ and do {
      print "blue = 0000FF\n";
      last;
   };
   /green/ and do {
      print "green = 00FF00\n";
      last;
   };
   /red/ and do {
      print "red = FF0000\n";
      last;
   }
   print "I don't know what color that is\n";
   last;
}

-------------
Cuvou.com | My personal homepage
Project Fearless | My web blog
 
This is how I would do that switch statement, even given the new construct:

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]%hex_colors[/blue] = [red]([/red]
	[purple]blue[/purple]   => [red]'[/red][purple]0000FF[/purple][red]'[/red],
	[purple]green[/purple]  => [red]'[/red][purple]00FF00[/purple][red]'[/red],
	[purple]red[/purple]    => [red]'[/red][purple]FF0000[/purple][red]'[/red],
[red])[/red][red];[/red]

[black][b]my[/b][/black] [blue]$color[/blue] = [red]'[/red][purple]red[/purple][red]'[/red][red];[/red]

[olive][b]if[/b][/olive] [red]([/red][black][b]my[/b][/black] [blue]$hex[/blue] = [blue]$hex_colors[/blue][red]{[/red][blue]$color[/blue][red]}[/red][red])[/red] [red]{[/red]
	[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple][blue]$color[/blue] = [blue]$hex[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[red]}[/red] [olive][b]else[/b][/olive] [red]{[/red]
	[black][b]print[/b][/black] [red]"[/red][purple]I don't know what color that is[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[red]}[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
[/tt]

- Miller
 
It was an example. :p

In practical use my approach would be used where an if/elsif/elsif/elsif/.../else would otherwise be used. ;-)

-------------
Cuvou.com | My personal homepage
Project Fearless | My web blog
 
I know, and a good demonstration of the impracticality of the old construct. Nevertheless, I still stand by my assertion that most cases where a switch statement would be used in the old C++ paradigm, are solved a lot better with a hash or dispatch table when using perl.

We'll see. I may find myself grateful for the new feature soon enough and eating my words.

[wink]

- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top