Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Switch depreciated?

by ksublondie (Friar)
on Jul 01, 2016 at 20:02 UTC ( #1167020=perlquestion: print w/replies, xml ) Need Help??

ksublondie has asked for the wisdom of the Perl Monks concerning the following question:

I'm using the Perl::Critic module to scan my code. One of the returned violations is:

Found use of Switch. This module is deprecated by the Perl 5 Porters at line 15, column 1. Find an alternative module.

However, I'm not finding any documentation anywhere to substantiate this. The only thing remotely close that I've found is that perl 6 is supposed to have a built-in switch with different syntax...but I'm using using ActiveState v5.2x. According to the ActiveState PPM index, the build is available for my OS and perl versions. Plus, I'm not seeing anything on cpan saying it's depreciated. I know I can configure the Perl::Critic as needed, but this seems a bit too important to ignore.

Any ideas on 1) where this may be documented and 2) what I should be using instead?

Replies are listed 'Best First'.
Re: Switch depreciated?
by dasgar (Priest) on Jul 01, 2016 at 20:21 UTC

    A recent thread (Best option for "switch/case" functionality?) might provide you with some documentation links and alternatives.

    Personally, I've used Switch::Plain without issues, but it's limited functionality ("simple string or numeric equality tests") might not meet everyone's needs.

Re: Switch depreciated?
by ikegami (Pope) on Jul 01, 2016 at 20:10 UTC

    I'm not seeing anything on cpan saying it's depreciated.

    That's because it's not depreciated deprecated.

    perlcritic's claim that Switch was deprecated is incorrect. It was simply removed from the perl distribution. That said, it's its removal from the distribution that perlcritic is warning you about. The solution, however, isn't to switch to another module; it's simply to mark Switch as a dependency of your distributable so it gets installed by cpan (or cpanm or whatever).

    That said, many dislike Switch because it can issue misleading error messages because it's based on a source filter. You might want to look for an alternative anyway. (An if statement with a bunch of elsif clauses usually works just fine.)

Re: Switch depreciated?
by Laurent_R (Canon) on Jul 02, 2016 at 07:58 UTC
    One possible way to replace a switch:
    for my $age (qw/ 1 7 14 21 75/) { my $person = $age < 2 ? "baby" : $age < 13 ? "kid" : $age < 18 ? "teen" : $age < 65 ? "adult" : "senior"; print "$age : \t $person \n"; }
Re: Switch depreciated?
by shmem (Chancellor) on Jul 03, 2016 at 14:36 UTC

    Does your code(-base) use Switch;? If so, see Switch. It prominently states in the section NAME

    Switch - A switch statement for Perl, do not use if you can use given/when

    Also, see the BUGS section:

    There are undoubtedly serious bugs lurking somewhere in code this funky :-) Bug reports and other feedback are most welcome.
    May create syntax errors in other parts of code.
    On perl 5.10.x may cause syntax error if "case" is present inside heredoc.
    In general, use given/when instead. It were introduced in perl 5.10.0. Perl 5.10.0 was released in 2007.

    So the perl you are using has given/when, and you'd better scan your codebase, remove any use Switch and replace switch/case with given/when instead of silencing Perl::Critic.

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
Re: Switch depreciated?
by genio (Beadle) on Jul 05, 2016 at 02:22 UTC

    As are most things, Perl::Critic is configurable. All you have to do is set the policy.

    One of the things you'll find is that the default policy is probably no longer the best one. On IRC, the many questions and answers have allowed people there to build up their own policy in Perl::Critic::Freenode. I'd suggest you look into that.

    On Switch, though, it's frowned upon by most because it uses source filters instead of pluggable keywords. You can see the reasons in the description of Switch::Plain that was mentioned above.

    So, let's just pretend that Switch really is deprecated.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1167020]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (9)
As of 2020-02-17 12:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?










    Results (71 votes). Check out past polls.

    Notices?