Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

(RhetTbull) Re: something like switch(case) in Perl

by RhetTbull (Curate)
on Jan 23, 2002 at 08:23 UTC ( #140814=note: print w/ replies, xml ) Need Help??


in reply to something like switch(case) in Perl

As others have said here, perl does not have a built-in switch statement. However, TheDamian has written an excellent module (which will be a standard core module in future perl versions) called Switch.pm. It's very nice and perlish in the way it handles things (e.g. it doesn't treat everything like an int as the 'C' switch does). For example (from the module docs):

use Switch; switch ($val) { case 1 { print "number 1" } case "a" { print "string a" } case [1..10,42] { print "number in list" } case (@array) { print "number in list" } case /\w+/ { print "pattern" } case qr/\w+/ { print "pattern" } case (%hash) { print "entry in hash" } case (\%hash) { print "entry in hash" } case (\&sub) { print "arg to subroutine" } else { print "previous case not true" } }

Update: Here's an example similar to yours that might get you started. However, as talexb and japhy both mentioned, using coderefs in your hash might be a cleaner way of doing this.

use strict; use warnings; use Switch; my %house = ('CC' => 'My Name', 'AB' => 'AB Name', 'CD' => 'My friend', 'EF' => 'Who?'); foreach my $person (keys %house) { print "$person: "; switch($person) { case 'AB' { print "Mine!\n" } case 'CC' { print "Yours!\n" } case 'CD' { print "Friend!\n" } case %house { print "in house, but don't know him!\n" } else { print "I don't know what to do!\n" } } }


Comment on (RhetTbull) Re: something like switch(case) in Perl
Select or Download Code
Re: (RhetTbull) Re: something like switch(case) in Perl
by agustina_s (Sexton) on Jan 23, 2002 at 08:58 UTC
    Thanks... Will it be faster using the switch statement or will it be just the same?
Re: (RhetTbull) Re: something like switch(case) in Perl
by Masem (Monsignor) on Jan 23, 2002 at 09:40 UTC
    As a word of caution, Switch is *not* a great module; yes, it replicates what C has, but it's not efficient nor impervious to bugs. As I believe the lastest word on Perl 6 from Larry has implied, the lack of a good switch block in Perl 5 is very much apparent.

    Keep this in mind when working with this. Switch might work, but be prepared to find weird things happening if you ask it for too much.

    -----------------------------------------------------
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
    "I can see my house from here!"
    It's not what you know, but knowing how to find it if you don't know that's important

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://140814]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (7)
As of 2014-07-11 04:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (218 votes), past polls