Why is the Switch module deprecated in Perl?
Asked Answered
W

3

20

Why was the Switch module deprecated in Perl 5.12?

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

Windsor answered 13/4, 2010 at 14:54 Comment(0)
K
26

Perl 5.10 introduced a real switch called given-when

The old Switch used source filtering and had other limitations.

Kealey answered 13/4, 2010 at 14:56 Comment(7)
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...Dreadnought
@OMG, I mostly work in Perl 5.8 but IIRC you can use a for/when construct. for/case would seem weird.Rosariorosarium
@OMG Perl's given, and when operators don't really behave anything like other language's switch/case so they are given different names.Purchasable
Further, Switch was removed from the core in Perl 5.14.Tribal
Since then, The smartmatch family of features are now experimental and issue warnings since 5.18.0. I guess we're stuck with good-old for, if, elsif and else.Wither
just want to note that good to use something like HASH with CODEREFs. https://mcmap.net/q/662410/-how-to-group-cases-within-a-switch-statement-in-perlFrication
And it's being removed in 5.42Kermitkermy
R
18

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. However, in Learning Perl 7th Edition we removed that chapter since smart-matching is experimental as of v5.18.

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.

Roguery answered 15/4, 2010 at 9:19 Comment(0)
K
2

First of all, Switch is not deprecated. It's inclusion in core was deprecated in 5.12. All that means is that while Switch was bundled with Perl 5.8 and 5.10, it isn't bundled in Perl 5.14 and newer. It does NOT mean people should stop using the module. The module may still be installed from CPAN separately from perl.

Mind you, many including myself advise others to avoid using Switch, but that's independent of its deprecation from core, and it far predates 5.12. Using Switch can lead to very odd and hard to debug errors because it modifies your code before Perl sees it, all while providing minimal benefit if any at all.

For that reason, and because Perl started offering a native alternative (given-when), it was decided that Switch would no longer be bundled with perl.

Unfortunately, given-when has problems, and was retroactively marked as experimental in 5.18. It isn't safe to use these as they will be changed in a backwards-incompatible manner (or maybe removed) in the future.

Kermitkermy answered 10/2, 2017 at 17:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.