NAME
Switch::Perlish - A Perlish implementation of the switch statement
VERSION
1.0.0 - Initial release
SYNOPSIS
use Switch::Perlish;
switch $var, sub {
case 'foo',
sub { print "$var is equal to 'foo'\n" };
case 42,
sub { print "$var is equal to 42\n";
fallthrough };
case [qw/ foo bar baz /],
sub { print "$var found in list\n" };
case { foo => 'bar' },
sub { print "$var key found in hash\n" };
case \&func,
sub { print "$var as arg to func() returned true\n" };
case $obj,
sub { print "$var is method in $obj and returned true\n" };
case qr/\bfoo\b/,
sub { print "$var matched against foo\n" };
default
sub { print "$var did not find a match\n" };
};
BACKGROUND
If you're unfamiliar with switch then this is the best place to start. A switch statement is essentially syntactic sugar for an if/elsif/else chain where the same $variable
is tested in every conditional e.g:
my $foo = 'a string';
if($foo eq 'something') {
print '$foo matched "something"';
} elsif($foo eq 'a string') {
print '$foo matched "a string"';
} else {
print '$foo matched nothing';
}
This simply matches $foo
against a series of strings, then defaulting to the last else
block if nothing matched. An equivalent switch statement (using this module) would be:
use Switch::Perlish;
my $foo = 'a string';
switch $foo, sub {
case 'something',
sub { print '$foo matched "something"' };
case 'a string',
sub { print '$foo matched "a string"' };
default
sub { print '$foo matched nothing' };
};
So the first argument to switch
is the thing to be tested (in code above, $foo
), and the second argument is the block of tests. Each case
statement matches its first argument against $foo
, and if the match is successful, the associated block is executed, so running the above code outputs: $foo matched "a string"
. Note the use of semi-colon at the end of the switch
, case
and default
calls - they're just simple subroutine calls.
DESCRIPTION
This is a Perl-oriented implementation of the switch
statement. It uses smart-matching in case
s which can be configured and extended by the user. There is no magical syntax so switch
/case
/default
expect coderefs, which are most simply provided by anonymous subroutines. By default successful case
statements do not fall through[1]. To get fall through functionality call the fallthrough
subroutine explicitly.
[1] To 'fall through' in a case
block means that the switch
block isn't exited upon success
Smart Matching
The idea behind smart matching is that the given values are matched in an intelligent manner, so as to get a meaningful result regardless of the values' types. This allows for flexible code and a certain amount of "just do it" when using smart matching. Below is a basic example using smart matching (which is done implictly in case
) where a simple value is being matched against an array e.g
use Switch::Perlish;
my $num = $ARGV[0];
switch $num, sub {
case undef,
sub { die "Usage: $0 NUM\n" };
case [0 .. 10],
sub { print "Your number was between 0 and 10" };
case [11 .. 100],
sub { print "Your number was between 11 and 100" };
case [101 .. 1000],
sub { print "Your number was between 101 and 1000" };
default
sub { print "Your number was less than 0 or greater than 1000" };
};
So here the smart matching is checking for the existence of $num
in the provided arrays. In the above code ranges happen to be used, but any array would suffice. To see how different value types compare with each other see. Switch::Perlish::Smatch::Comparators, which lists all the default comparators.
The code behind this smart matching can be found in Switch::Perlish::Smatch which itself delegates to the appropriate comparator subroutine depending on the value types. See Switch::Perlish::Smatch for more details on the smart matching implementation and how it can be extended.
SUBROUTINES
- switch( $topic, $block );
-
Execute the given
$block
allowingcase
statements to access the$topic
. - case( $match, $block );
-
If
$topic
smart-matches successfully against$match
then execute$block
and exit fromswitch
. NB: this cannot be called outside ofswitch
, if you want to use smart matching functionality, see. Switch::Perlish::Smatch. - default( $block )
-
Execute
$block
and exit fromswitch
. NB: this cannot be called outside ofswitch
. - fallthrough()
-
Fall through the the current
case
block. NB: this cannot be called outside ofswitch
.
Globals
- $SWITCH
-
The current
switch
block. - $CASE
-
The current
case
block. - $TOPIC
-
The current topic block, also aliased to
$_
. - $MATCH
-
The current thing being matched against.
SEE. ALSO
perlfaq7
Switch::Perlish::Smatch::Comparators
TODO
Use a version control system
Implement localizing comparators
Test with earlier versions of
perl
AUTHOR
Dan Brook <cpan@broquaint.com>
COPYRIGHT
Copyright (c) 2005, Dan Brook. All Rights Reserved. This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.