develooper Front page | perl.beginners | Postings from February 2002

Re: Does Perl have "Case" Statements?

Thread Previous | Thread Next
From:
Brett W. McCoy
Date:
February 28, 2002 13:53
Subject:
Re: Does Perl have "Case" Statements?
Message ID:
Pine.LNX.4.43.0202281652450.5598-100000@chapelperilous.net
On Thu, 28 Feb 2002, Chris wrote:

> I have been looking in the Learning Perl book, and cannot find it.

There isn't a syntactical element in Perl to do case statements (aka
switch in C/C++ & Java).  However, they can be done in a variety of ways:

use the Switch.pm module

or use the aliasing for does into $_:

SWITCH: for($somevar) {
	/value1/ && do {
	  ...
	  last SWITCH;
	};

	/value2/ && do {
	  ...
	  last SWITCH;
	};

	...

	die "Invalid value $somevar\n";
}

There are many ways to do this in Perl, which is one of the reasons given
for not having an explicit switch operator.

-- Brett
                                          http://www.chapelperilous.net/
------------------------------------------------------------------------
<SirDibos> does Johnie Ingram hang out here on IRC?


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About