Why was Switch deprecated in Perl?
I know that a switch
/case
be made with elsif
, but I don't like that very much.
Why was Switch deprecated in Perl?
I know that a switch
/case
be made with elsif
, but I don't like that very much.
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
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.