For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > February 2007 > FAQ 7.22 How do I create a switch or case statement?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author FAQ 7.22 How do I create a switch or case statement?
PerlFAQ Server

2007-02-27, 10:03 pm

This is an excerpt from the latest version perlfaq7.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .

--------------------------------------------------------------------

7.22: How do I create a switch or case statement?

This is explained in more depth in the perlsyn. Briefly, there's no
official case statement, because of the variety of tests possible in
Perl (numeric comparison, string comparison, glob comparison, regex
matching, overloaded comparisons, ...). Larry couldn't decide how best
to do this, so he left it out, even though it's been on the wish list
since perl1.

Starting from Perl 5.8 to get switch and case one can use the Switch
extension and say:

use Switch;

after which one has switch and case. It is not as fast as it could be
because it's not really part of the language (it's done using source
filters) but it is available, and it's very flexible.

But if one wants to use pure Perl, the general answer is to write a
construct like this:

for ($variable_to_test) {
if (/pat1/) { } # do something
elsif (/pat2/) { } # do something else
elsif (/pat3/) { } # do something else
else { } # default
}

Here's a simple example of a switch based on pattern matching, this time
lined up in a way to make it look more like a switch statement. We'll do
a multiway conditional based on the type of reference stored in
$whatchamacallit:

SWITCH: for (ref $whatchamacallit) {

/^$/ && die "not a reference";

/SCALAR/ && do {
print_scalar($$ref);
last SWITCH;
};

/ARRAY/ && do {
print_array(@$ref);
last SWITCH;
};

/HASH/ && do {
print_hash(%$ref);
last SWITCH;
};

/CODE/ && do {
warn "can't print function ref";
last SWITCH;
};

# DEFAULT

warn "User defined type skipped";

}

See "perlsyn/"Basic BLOCKs and Switch Statements"" for many other
examples in this style.

Sometimes you should change the positions of the constant and the
variable. For example, let's say you wanted to test which of many
answers you were given, but in a case-insensitive way that also allows
abbreviations. You can use the following technique if the strings all
start with different characters or if you want to arrange the matches so
that one takes precedence over another, as "SEND" has precedence over
"STOP" here:

chomp($answer = <> );
if ("SEND" =~ /^\Q$answer/i) { print "Action is send\n" }
elsif ("STOP" =~ /^\Q$answer/i) { print "Action is stop\n" }
elsif ("ABORT" =~ /^\Q$answer/i) { print "Action is abort\n" }
elsif ("LIST" =~ /^\Q$answer/i) { print "Action is list\n" }
elsif ("EDIT" =~ /^\Q$answer/i) { print "Action is edit\n" }

A totally different approach is to create a hash of function references.

my %commands = (
"happy" => \&joy,
"", => \&sullen,
"done" => sub { die "See ya!" },
"mad" => \&,
);

print "How are you? ";
chomp($string = <STDIN> );
if ($commands{$string}) {
$commands{$string}->();
} else {
print "No such command: $string\n";
}



--------------------------------------------------------------------

The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.

--
Posted via a free Usenet account from http://www.teranews.com

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com