http://qs321.pair.com?node_id=140814


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" } } }