Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Modern exposition of "case" or Switch in perl 5.34?

by kcott (Archbishop)
on Jul 03, 2021 at 01:58 UTC ( [id://11134610]=note: print w/replies, xml ) Need Help??


in reply to Modern exposition of "case" or Switch in perl 5.34?

G'day symgryph,

"I have read a lot about NOT using the 'switch' statement of yore, ..."

You need to be a bit careful with terminology. Perl does not have nor, as far as I know, has ever had a 'switch' statement. It does have "The 'switch' feature". There is a CPAN Switch module which provides a 'switch' function. I'm going to assume that you're talking about the Switch module.

The BUGS and LIMITATIONS sections of Switch both provide a number of reasons to avoid it. I would strongly recommend against using it in production code.

"... but saw some comments on using 'when' instead."

I'll assume that's from the Switch BUGS section: "In general, use given/when instead.".

When that was written, it was probably an entirely reasonable statement; however, in 5.18.0 'given', 'when, and the associated '~~' operator, were made experimental: "perl5180delta: The smartmatch family of features are now experimental". At 5.34.0 they remain experimental:

$ perl -v | head -2 | tail -1 This is perl 5, version 34, subversion 0 (v5.34.0) built for cygwin-th +read-multi $ perl -E 'say 1~~2 ? "yes" : "no"' Smartmatch is experimental at -e line 1. no $ perl -E 'my $x = 1; given ($x) { when (2) { say "yes" } default { sa +y "no" } }' given is experimental at -e line 1. when is experimental at -e line 1. no

Again, I would not use these in production code.

"Is there a modern howto on doing a case like construct in perl 5.34?"

— Ken

Replies are listed 'Best First'.
Re^2: Modern exposition of "case" or Switch in perl 5.34?
by ikegami (Patriarch) on Jul 04, 2021 at 11:11 UTC

    At 5.34.0 they remain experimental

    And not for no reason. The design of smartmatch is considered fundamentally broken. They want to abandon this experiment, or at least redesign smart matching. The removal of smartmatching is being held back, at least for now, by the number of users of the experimental feature.

    Seeking work! You can reach me at ikegami@adaelis.com

      "The removal of smartmatching is being held back, at least for now, by the number of users of the experimental feature."

      Yes. Many, including myself, used smartmatch when it was first introduced in 5.10 (when it was not experimental). When it was made experimental in 5.18, many (again, including myself) simply followed the workaround provided in perl5180delta:

      no if $] >= 5.018, warnings => "experimental::smartmatch";

      This removed the copious "experimental" warnings and the code continued to run, without any problems, as it had done previously.

      The result is that now there is a large body of code in CPAN, as well as commercial and private libraries, that use smartmatch. I certainly have private code, still in current use, that uses it; it's entirely possible that commercial code which I wrote prior to 5.18 also included it.

      I suppose the lesson to be learnt here is to make all new features experimental until substantial in-the-field testing indicates no problems. This does seem to be the case for many features (e.g. try/catch in 5.34) but not all (e.g. chained comparisons in 5.32).

      — Ken

      The removal of smartmatching is being held back, at least for now, by the number of users of the experimental feature

      Perhaps that shows how useful in demand the feature is.

        That doesn't mean it should be a buggy one.

        Seeking work! You can reach me at ikegami@adaelis.com

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11134610]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-04-19 23:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found