tags:

views:

232

answers:

2

Why was Switch deprecated in Perl?

I know that a switch/case be made with elsif, but I don't like that very much.

+10  A: 

The previous* major version release of Perl 5 introduced a real switch called given-when

The old Switch used source filtering and had other limitations.

* Happy day: version 12 of Perl 5 was released this morning

David Dorward
I was rather hoping that Perl would ditch the `given`/`when` keywords and use something more similar other languages... like `switch`/`case`. I would say that those aren't "Huffman coded" (Larry's phrase from Perl 6 Apocalypse 3) but they're the same length...
R. Bemrose
@OMG, I mostly work in Perl 5.8 but IIRC you can use a `for`/`when` construct. `for`/`case` would seem weird.
daotoad
@OMG Perl's `given`, and `when` operators don't really behave anything like other language's `switch`/`case` so they are given different names.
Brad Gilbert
+2  A: 

The original Switch uses a source filter to do its work, and that's often a bad idea. Essentially, it pre-processes your literal source to create new code before perl compiles it. The module was never really intended to be heavily used, and it was more of a proof of concept to figure out what a real Perl feature could look like.

Perl 5.10 added the given-when construct to do what most people want from a switch-case, but it does quite a bit more. Learning Perl, 5th Edition devotes an entire chapter to it along with smart matching.

You can't make a Perl given-when with the if-elsif-else constructs. given-when lets you execute multiple blocks and well as add interstitial code. With if-elsif-else you execute exactly one branch.

brian d foy