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

Hi, I am using the latest Strawberry perl 5.10.0.1 and although 5.10.0.1 > 5.10, the smart match operator does not seem to be supported. The following code lines come from feature.pm:
my %feature = ( switch => 'feature_switch', say => "feature_say", state => "feature_state", );
So, no smart match here. Of course I get the error
Feature "~~" is not supported by Perl 5.10.0 at ...
when trying "use feature '~~';". It seems to be partially working, though, even if I don't declare the usage. E.g. the following test program:
use feature qw / say /; @a = (1, 17, 4); say "first match succeeds" if (@a ~~ 17); say "second match fails" unless ((1, 17, 4) ~~ 17);
Prints out
first match succeeds second match fails
I would really like to use the smart match operator, too. Do you know when this will be fully supported in Strawberry Perl or if I overlooked something?

Replies are listed 'Best First'.
Re: Smart match operator support in Strawberry perl 5.10.0.1 (array vs. list)
by almut (Canon) on May 22, 2008 at 10:24 UTC
    It seems to be partially working, though

    (I'm reading that that you would like it to match in the list case (1, 17, 4) ~~ 17, too.)

    I think it doesn't treat arrays and lists the same way (from "Smart matching in detail" in perlsyn):

    $a $b Type of Match Implied Matching Code ====== ===== ===================== ============= ... Array Num array contains number grep $_ == $b, @$a ...

    If you have a list instead of an array, it interprets it in scalar context, i.e. it just uses the last element, here 4:

    say "second match succeeds" if ((1, 17, 4) ~~ 4);

    I personally find this somewhat counterintuitive, too, but I suspect the language designers have had their reason to make it behave that way...

      Hi, thanks for your replies, did not have time to check them up to now! So it seems like I don't need to say "use feature '~~';" at all, that's o.k.

      On the other hand, I find the distinction made in the current context between a literal list and an array variable having the same list as value counterintuitive and it is the first time I have tripped over such a distinction in Perl. I never thought of the last element of a list to represent in some way the value of the list, which obviously happens in the smart match context as shown in almut's last example.

      I played a little more around with this and found that such a distinction is not made in case we are dealing with array references:

      use feature qw / say /; sub check_match { $n++; my $expr = shift; say ("$n th match" . (eval($expr) ? ' succeeds' : ' fails')); } @a = (1, 17, 4); check_match('@a ~~ 17'); check_match('(1, 17, 4) ~~ 17'); $b = [1, 17, 4]; check_match('$b ~~ 17'); check_match('[1, 17, 4] ~~ 17');
      results in the following output:
      1 th match succeeds 2 th match fails 3 th match succeeds 4 th match succeeds
      Now I am really puzzled, I would find it more natural if either both of cases 2 and 4 succeeded or both failed.
Re: Smart match operator support in Strawberry perl 5.10.0.1
by moritz (Cardinal) on May 22, 2008 at 09:20 UTC
    I don't know about Strawberry, but in vanilla perl 5.10.0 you can use smart matching without any use feature directive. This was wrongly documented in older development versions, but should be fixed now. (If it isn't, please tell us where you read that).
    $ perl5.10.0 --version This is perl, v5.10.0 built for i486-linux-gnu-thread-multi [...] $ perl5.10.0 -wle 'print "YaY" if "a" ~~ m/./' YaY

    Update: I found the doc patch that fixed perlsyn.

Re: Smart match operator support in Strawberry perl 5.10.0.1
by Khen1950fx (Canon) on May 22, 2008 at 10:17 UTC
    I'm new to the smart match operator, but I took a stab at it. Does this help?

    #!/usr/bin/perl use strict; use warnings; use 5.010; my @a = (1, 17, 4); say "first match suceeds" if (@a ~~ 17); say "second match fails" unless ([1, 17, 4] ~~ 17);