On Sat, 11 Jun 2005, Jeff 'japhy' Pinyan wrote:
> On Jun 11, Ron Smith said:
>
> > Does Perl have the equivalent of a case statement or a switch
> > statement. I'm trying to avoid a bunch of "if-then" statements.
> > I'm seeing posts regarding "use switch", but I want to make sure
> > it's not a deprecated practice. I'm using Perl -v 5.8.0.
>
> The Switch.pm module isn't standard with Perl, but is a way to do it...
Yes. With the standard Switch.pm module :-)
$ perldoc -q switch
Found in /System/Library/Perl/5.8.1/pods/perlfaq7.pod
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. [...]
But then, the same article goes on to say...
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
}
And more examples follow in the same document.
--
Chris Devers
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>