in reply to Re: No braces
in thread No braces

You're just creating more work for yourself by using Switch.pm. If it screws up the parsing, you now have to change code (which may be completely unrelated to the code that actually uses a switch statement) just to work around bugs in Switch.pm. That's a lot more work for something that is little more than syntax sugar and is easily replaced with a multitude of common Perl idioms. Even if you make all the workarounds in the current version of Switch.pm, new versions may introduce brand new bugs for you to work around.

The correct solution is to avoid Switch.pm in the first place for serious code.

----
: () { :|:& };:

Note: All code is untested, unless otherwise stated

Replies are listed 'Best First'.
Re: No braces
by Abigail-II (Bishop) on Feb 11, 2004 at 18:19 UTC
    If it screws up the parsing, you now have to change code (which may be completely unrelated to the code that actually uses a switch statement) just to work around bugs in Switch.pm.
    You sound as if that's a common case. How often do you write code that contains something that looks like a 'switch', but isn't?
    That's a lot more work for something that is little more than syntax sugar and is easily replaced with a multitude of common Perl idioms.
    The same could be said about IO::Socket or CGI. Or about almost any module out there. And any module may contain bugs you will have to code around. There hasn't been a perl released that doesn't contain bugs someone has to code around.

    Abigail

      As far as IO::Socket and CGI.pm, my life is far easier using them. Neither of these do particularly simple things. In the case of CGI.pm, it's easy to do a replacement wrong in subtle ways. If either one has bugs, I can at least have a reasonable assurance that the bug will be localized in the code that actually uses the given module.

      None of this is true for Switch.pm. Replacing a Switch.pm solution with a hand-coded one is easy. In the common case of simply checking equivilance against a number or string, it's trivial to replace.

      # Comes right out of the Switch.pm synopsis switch ($val) { case 1 { print "number 1" } case "a" { print "string a" } case [1..10,42] { print "number in list" } case (@array) { print "number in list" } case /\w+/ { print "pattern" } case qr/\w+/ { print "pattern" } case (%hash) { print "entry in hash" } case (\%hash) { print "entry in hash" } case (\&sub) { print "arg to subroutine" } else { print "previous case not true" } } # # Instead, write the below. Yeah, it's a little sloppy, # but I just want to show that it can be done. # for $i ($val) { $i == 1 && do { print "number 1"; last }; $i eq "a" && do { print "string a"; last }; grep { $i == $_ } @{[1..10,42]} && do { print "number in list"; la +st }; grep { $i == $_ } @array && do { print "number in list"; la +st }; $i =~ /\w+/ && do { print "pattern"; last }; $i =~ qr/\w+/ && do { print "pattern"; last }; exists $hash{$i}&& do { print "entry in hash"; last }; exists $hash->{$i} && do { print "entry in hash"; last }; sub($i) && do { print "arg to subroutine"; last }; print "previous case not true"; }

      (Some of the above might not be exactly right, but it should be close).

      With some modifications needed to support fall through. A few of the Switch.pm supported cases don't work translate as easily to a hand-rolled solution, but these aren't that much harder, and they aren't too common anyway.

      If it screws up the parsing, you now have to change code (which may be completely unrelated to the code that actually uses a switch statement) just to work around bugs in Switch.pm.

      You sound as if that's a common case. How often do you write code that contains something that looks like a 'switch', but isn't?

      It is quite common that Switch.pm create bugs for any non-trivial program. Damian might be brilliant, but I don't think he or Rafael Garcia-Suarez (current maintainer of Switch.pm) are willing to put the effort into implementing a Perl parser compilaint with whatever the current version of perl is. Time spent doing that is better used for Perl6 or Ponie, IMHO.

      If Switch.pm gave an efficiency bonus like a C switch does, I might see an argument for using it if you've already profiled your code and know you need to optimize a large case structure. But it doesn't provide any such bonus. As something that can only be considered a minor conviance, I'd rather leave it behind.

      ----
      : () { :|:& };:

      Note: All code is untested, unless otherwise stated