=head1 TITLE
Builtin switch statement
=head1 VERSION
Maintainer: Damian Conway <[EMAIL PROTECTED]>
Date: 4 August 2000
Version: 1.00
Mailing List: [EMAIL PROTECTED]
Number: 22
=head1 ABSTRACT
This RFC proposes a syntax and semantics for an explicit case mechanism for
Perl. The syntax is minimal, introducing only the keywords C<switch> and
C<case> and conforming to the general pattern of existing Perl control
structures. The semantics are particularly rich, allowing any one (or
more) of nearly 30 forms of matching to be used when comparing a switch
value with its various cases.
=head1 BACKGROUND
In seeking to devise a "Swiss Army" case mechanism suitable for Perl,
it is useful to generalize the switch/case notion of distributed conditional
testing as far as possible. Specifically, the concept of "matching"
between the switch value and the various case values need not be
restricted to numeric (or string or referential) equality, as it is in other
languages. Indeed, as Table 1 illustrates, Perl
offers at least sixteen different ways in which two values could
generate a match.
=head2 Table 1: Matching a switch value (C<$s>) with a case value (<$c>)
Switch Case Type of Match Implied Matching Code
Value Value
====== ===== ===================== =============
number I<same> numeric or referential C<match if $s == $c;>
or ref equality
other other string equality C<match if $s eq $c;>
non-ref non-ref
scalar scalar
string regexp pattern match C<match if $s =~ /$c/;>
array scalar array entry existence C<match if 0E<lt>=$c && $cE<lt>@$s;>
ref array entry definition C<match if defined $s-E<gt>[$c];>
array entry truth C<match if $s-E<gt>[$c];>
array array array intersection C<match if intersects(@$s, @$c);>
ref ref (apply this table to
all pairs of elements
C<$s-E<gt>[$i]> and
C<$c-E<gt>[$j]>)
array regexp array grep C<match if grep /$c/, @$s;>
ref
hash scalar hash entry existence C<match if exists $s-E<gt>{$c};>
ref hash entry definition C<match if defined $s-E<gt>{$c};>
hash entry truth C<match if $s-E<gt>{$c};>
hash regexp hash grep C<match if grep /$c/, keys %$s;>
ref
sub scalar return value definition C<match if defined $s-E<gt>($c);>
ref return value truth C<match if $s-E<gt>($c);>
sub array return value definition C<match if defined $s-E<gt>(@$c);>
ref ref return value truth C<match if $s-E<gt>(@$c);>
In reality, Table 1 covers 29 alternatives, because only the equality and
intersection tests are commutative; in all other cases, the roles of
the C<$s> and C<$c> variables could be reversed to produce a
different test. For example, instead of testing a single hash for
the existence of a series of keys (C<match if exists $s-E<gt>{$c}>),
one could test for the existence of a single key in a series of hashes
(C<match if exists $c-E<gt>{$s}>).
As L<perltodo> observes, a Perl case mechanism must support all these
"ways to do it".
=head2 DESCRIPTION
Adding a switch statement would require two new control structures:
C<switch> and C<case>. The C<switch> statement takes a single
parenthesized argument (the "switch value") of any scalar type,
including any reference type. A C</.../> or C<m{...}> pattern may also
be specified and is automagically converted to a C<qr/.../>. A hash or array
may also be specified and is automatically enreferenced.
Regardless of the type of argument, its value is stored as the
current switch value in an inaccessible localized control variable.
The switch value is followed by a block, which may contain any statements,
including zero or more C<case> statements.
The C<case> statement takes a single scalar argument (which need be parenthesized
only if it's a variable), followed by a block.
The C<case> statement selects the
appropriate type of matching between that argument and the current switch value,
as determined by the respective types of the switch value and the C<case> argument
(see Table 1). If the match is successful, the block associated
with the C<case> statement is executed.
In most other respects, the C<case> statement is semantically identical
to an C<if> statement.
For example, a C<case> statament can be followed by an C<else>
clause, and can be used as a postfix statement qualifier.
However, once the block of any C<case> statement finishes executing,
control is immediately transferred to the end of the enclosing C<switch>
statement. The same effect may be obtained by executing a C<last> within
the block of the C<switch>. This means that, normally, there is no C-like
"fall-through" from a Perl C<case> statement. For example:
my $val = 7;
switch ($val) {
case [0..9] { print "single digit\n" }
case [10..] { print "digits\n"; last }
case 7 { print "lucky seven!\n" }
}
would never print "lucky seven", since the first C<case> will always
succeed and transfer control out of the C<switch>.
Howeverm it I<is> possible to cause a C<case> statement to "fall-through". If a
C<next> is executed within a C<case> block, control is immediately
transferred to the statement following the C<case> block.
For example:
switch ($count) {
case 0 { print "nothing\n"; next }
print "fell through!\n";
case /^\d$/ { print "single\n"; next }
print "fell through again!\n";
case sub{$_[0]%2} { print "odd\n" }
else { print "even\n" }
}
=head2 Larger examples
%special = ( woohoo => 1, d'oh => 1 );
while (<>) {
switch ($_) {
case (%special) { print "homer\n"; last } # if
$special{$_}
case /a-z/i { print "alpha\n"; last } # if $_ =~
/a-z/i
case [1..9] { print "small num\n"; last } # if $_ in
[1..9]
case sub { $_[0] >= 10 } { # if $_ >= 10
my $age = <>;
switch (sub { $_[0] < $age } ) {
case 20 { print "teens\n"; next }
case 30 { print "twenties\n"; next }
else { print "history\n"; next }
}
}
}
print "must be punctuation\n" case /\W/; # if $_ ~= /\W/
}
The use of intersection tests against an array reference is particularly
useful for aggregating integral cases:
sub classify_digit
{
switch ($_[0]); case 0 { return 'zero' }
case [2,4,6,8] { return 'even' }
case [1,3,4,7,9] { return 'odd' }
case /[A-F]/i { return 'hex' }
}
=head1 IMPLEMENTATION
S.E.P.
A sample (pure Perl) implementation will be on the CPAN RSN.
There is probably also good opportunity for compile-time optimization
for constant (or inferrable) switch/case values.
=head1 SEE ALSO
RFC on higher order functiosn
RFC on lazy lists
=head1 REFERENCES
perltodo, perlsyn, perlfaq7
Christiansen, T., Message posted to comp.lang.perl.misc, September 1998,
archived at http://language.perl.com/misc/fmswitch
Torkington, N., Message posted to perl5-porters mailing list, February 1997,
archived at
http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1997-02/msg01558.html
Barr, G., Message posted to perl5-porters mailing list, October 1997, archived at
http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1997-10/msg00057.html