in reply to Smart match operator support in Strawberry perl 5.10.0.1

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...

Replies are listed 'Best First'.
Re^2: Smart match operator support in Strawberry perl 5.10.0.1 (array vs. list)
by jds17 (Pilgrim) on May 22, 2008 at 15:57 UTC
    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.