in reply to Switch/case as a dispatch table with C-style fall-through
While I don't have much in the way of implementation suggestions, I do have a few requirements that I'd like to see from a switch statement. You may already implement some of these - I haven't checked.
0 and 1 must fall through to 2, but not 3. It also matters with the case of regex matches (q.v. below).switch (i) { 0,1: foo(); 2: bar(); break; 3: baz(); break; };
Taking a page from Class::MakeMethods, it might be useful to have each case be an arrayref. The last value is the action to take. All others are the case options. If the first value begins with '--', then it's an optional modifier to how to handle the next case option. So, maybe something like:
my $switch = Switch->new( [ '5', sub {} ], # Matches '005' [ '--string-match', '6', sub {} ], # Doesn't match '006', [ qr/floober/, sub {} ] # regex match [ sub { $foo->bar( @_ ) }, sub {} ] # coderef [ 1 .. 3, 8 .. 20, sub {} ] # Multiple cases );
That would be a useful module.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Switch/case as a dispatch table with C-style fall-through
by Roy Johnson (Monsignor) on May 12, 2005 at 14:06 UTC |