Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Experimental warnings with given/when

by Bod (Parson)
on Nov 30, 2020 at 22:54 UTC ( [id://11124422]=perlquestion: print w/replies, xml ) Need Help??

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

given and when were introduced back in Perl v5.10 I believe. But they have not been deprecated...I know deprecation of an accepted feature is very, very rare in Perl but these are marked as experimental. I've been looking at them more for curiosity than any need to use them.

Even with use experimental "switch"; they give a warning. I wasn't sure if this was on STDOUT or STDERR so I redirected STDERR and found that the warnings are not on there! I thought that use feature ... gave the warnings and use experimental ... didn't but in both cases I am getting the warnings. So, presumably, use warnings; overrides the experimental part.

#use v5.10; #use feature "switch"; use experimental "switch"; use strict; use warnings; # Redirect STDERR open my $fh, '>', 'error.log'; *STDERR = $fh; my $i = 3; given($i) { print "$i\n" when $i < 4; } # Prove STDERR is redirected with runtime error my $error = 10 / 0;

Given that deprecation is so very rare, is it safe to suppress the warnings and use some of the experimental features?

Replies are listed 'Best First'.
Re: Experimental warnings with given/when
by dave_the_m (Monsignor) on Nov 30, 2020 at 23:56 UTC
    You raise three points. First, the warnings aren't being disabled via 'use experimental "switch";' because you need to enable warnings with 'use warnings' before disabling selected warnings.

    Second, I don't know why the warnings aren't going to where ever STDERR is being redirected to.

    Third, smartmatch and switch were made retropectively experimental because it was widely agreed that they were broken in concept in many ways, but a consensus has never been reached on how to fix them. But the expectation is that at some point their behaviour may change, possibly radically, or they may be removed altogether.

    Dave.

      With my C/C++ (and various other language) background I like given/when a lot. Making smart match a whole lot dumber would pretty much fix the problem in my book, but that's a fairly limited book and it's probably missing important pages.

      Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
        > Making smart match a whole lot dumber would pretty much fix the problem in my book

        definitely, many people want an in operator without needing to resort to temporary hash tricks

        DB<19> use experimental 'smartmatch'; say 1 ~~ [1,2,3] 1

        what's stopping them is the huge table of cases.

        "dumbmatch" should be much simpler to understand.

        perlop#Smartmatch-Operator

        Left Right Description and pseudocode =============================================================== Any undef check whether Any is undefined like: !defined Any Any Object invoke ~~ overloading on Object, or die Right operand is an ARRAY: Left Right Description and pseudocode =============================================================== ARRAY1 ARRAY2 recurse on paired elements of ARRAY1 and ARRAY2[2 +] like: (ARRAY1[0] ~~ ARRAY2[0]) && (ARRAY1[1] ~~ ARRAY2[1]) && ... HASH ARRAY any ARRAY elements exist as HASH keys like: grep { exists HASH->{$_} } ARRAY Regexp ARRAY any ARRAY elements pattern match Regexp like: grep { /Regexp/ } ARRAY undef ARRAY undef in ARRAY like: grep { !defined } ARRAY Any ARRAY smartmatch each ARRAY element[3] like: grep { Any ~~ $_ } ARRAY Right operand is a HASH: Left Right Description and pseudocode =============================================================== HASH1 HASH2 all same keys in both HASHes like: keys HASH1 == grep { exists HASH2->{$_} } keys HASH1 ARRAY HASH any ARRAY elements exist as HASH keys like: grep { exists HASH->{$_} } ARRAY Regexp HASH any HASH keys pattern match Regexp like: grep { /Regexp/ } keys HASH undef HASH always false (undef can't be a key) like: 0 == 1 Any HASH HASH key existence like: exists HASH->{Any} Right operand is CODE: Left Right Description and pseudocode =============================================================== ARRAY CODE sub returns true on all ARRAY elements[1] like: !grep { !CODE->($_) } ARRAY HASH CODE sub returns true on all HASH keys[1] like: !grep { !CODE->($_) } keys HASH Any CODE sub passed Any returns true like: CODE->(Any) Right operand is a Regexp: Left Right Description and pseudocode =============================================================== ARRAY Regexp any ARRAY elements match Regexp like: grep { /Regexp/ } ARRAY HASH Regexp any HASH keys match Regexp like: grep { /Regexp/ } keys HASH Any Regexp pattern match like: Any =~ /Regexp/ Other: Left Right Description and pseudocode =============================================================== Object Any invoke ~~ overloading on Object, or fall back to... Any Num numeric equality like: Any == Num Num nummy[4] numeric equality like: Num == nummy undef Any check whether undefined like: !defined(Any) Any Any string equality like: Any eq Any

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

      Second, I don't know why the warnings aren't going to where ever STDERR is being redirected to.

      I was very curious to understand where Perl sends warning which is why I redirected STDERR. This is not something I do often, hence the deliberate division by zero runtime error to prove that it really was redirected.

      # STDOUT given is experimental at test.pl line 17. when is experimental at test.pl line 18. # STDERR Illegal division by zero at test.pl line 22.

      So it seems that warnings are sent to STDOUT

        Are you sure? With perl 5.20.3:

        $ perl -wE 'given (1) {2}' given is experimental at -e line 1. Useless use of a constant (2) in void context at -e line 1. $ perl -wE 'given (1) {2}' 2> /tmp/err $ cat /tmp/err given is experimental at -e line 1. Useless use of a constant (2) in void context at -e line 1. $

        🦛

Re: Experimental warnings with given/when
by kcott (Archbishop) on Nov 30, 2020 at 23:53 UTC

    G'day Bod,

    "I've been looking at them more for curiosity than any need to use them."

    They became experimental in 5.18.0 - see "perl5180delta". Of immediate interest to you, in that rather lengthy page, are probably these sections: "The smartmatch family of features are now experimental"; and, "New mechanism for experimental features".

    Also look at the documentation for the pragmata: feature and experimental. The "Ordering matters" section in the latter holds the answer to your problem.

    Here's a cutdown version of what you're doing:

    $ perl -E ' use strict; use experimental "switch"; use warnings; given (1) { say "Hello" } ' given is experimental at -e line 6. Hello

    Taking the advice of the Ordering matters section, and the warning disappears:

    $ perl -E ' use strict; use warnings; use experimental "switch"; given (1) { say "Hello" } ' Hello

    — Ken

Re: Experimental warnings with given/when
by Anonymous Monk on Dec 01, 2020 at 00:19 UTC

    Hi

    What is the warning that they give ? Copy/paste the warning

    Without  use experimental "switch"; I get a syntax error, which is not a warning

    If you look at https://perldoc.perl.org/warnings you can see deprecated is a seperate category from experimental

      What is the warning that they give ? Copy/paste the warning

      given is experimental at test.pl line 17. when is experimental at test.pl line 18.

      These are on STDOUT not STDERR

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-04-20 04:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found