A shiny new switch()

broquaint on 2005-05-21T03:17:06

I've just finished up a new switch module which should be coming to a CPAN mirror near you soon (and can be found here presently). It goes by the name of Switch::Perlish and can be pointed to when foreigners ask where switch is in perl. Here's some obligatory example code as taken from the documentation:

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" }; };



broquaint out


Switch

malte on 2005-05-21T07:29:45

How does it compare to Switch?

That module seems to do a better Job at simulating the classic switch style but uses a source filter. Your module could probably benefit a lot from the & prototype. Or is there a reason you didnt use prototypes?

Re:Switch

sigzero on 2005-05-21T14:04:27

Plus Switch is included in my ActiveState build. There is also a cool swith that lets you use switch statements like Perl6.

Re:Switch

broquaint on 2005-05-21T14:13:23

Good questions!
How does it compare to Switch?
I'd like to think that in terms of complexity S::P is a simpler module because it uses Perl's native syntax. It also has an extensible smart matching system, so it can be used with your own objects. The main intention of the module is in the name - a perlish implementation of switch, so it should be subject to all the wonderful features and quirks of perl.
That module seems to do a better Job at simulating the classic switch style but uses a source filter. Your module could probably benefit a lot from the & prototype. Or is there a reason you didnt use prototypes?
Indeed, Mr. Conway's Switch provides a much more natural syntax for switch, but through the magic for source filters. Unfortunately, this means the actual switch code can be rather hairy to debug and means that it goes unused in many production systems.

As for the use of the & prototype, there are a few reasons why I didn't use it. Firstly, it doesn't work how I would like it to - it is desigined to enable one to mimic Perl's builtins, so the block goes first. If there is a way to use the & prototype, and utilise its magical bare block functionality in a different position than the first argument, then I don't know about it :) Also prototypes cripple the the free-form usage of functions, which isn't very perlish at all, which strictly goes against the, er, perlishness of this module.

Perhaps this should all be in the documentation ...

Re:Switch

Aristotle on 2005-05-21T17:46:56

I very much like modules which have a section on how they differ from similar preexisting modules. It shows the author didn’t just mindlessly go to work. Of course, in case of Switch::Perlish I know enough about the competition to know exactly why you did what you did (great job, btw! neat ways to exploit the native syntax), but not everyone might have that benefit.

Re:Switch

broquaint on 2005-05-22T11:35:03

A very good idea indeed, I'll add that in the next update. Thanks!

Re:Switch

broquaint on 2005-05-21T14:22:53

With regards to prototypes, I should probably also point out Tom Christansen's Far More Than Everything You've Ever Wanted to Know about Prototypes in Perl, for the folk out there who aren't familiar with the document.