in reply to smart match operator should be smarter!

First of all, what's with all the useless parens?

my @numbers = (2); => my @numbers = 2; my @other = (2); => my @other = 2; @numbers ~~ (2) => @numbers ~~ 2 my @empty = (); => my @empty;

my @numbers = 2; @numbers ~~ 2 being false is to be expected. It was necessary to make ~~ non-commutative to fix bugs. In this case, since you're comparing against a simple scalar, a scalar comparison is made. You want 2 ~~ @numbers.


As for when (()), it should give an error if it doesn't do anything useful. I'll see about reporting a bug. ( On second thought, empty list in scalar context returns undef. I don't think anything can be done about that. )


Finally, keep in mind that when you specify an array, it's a reference to the array that's provided to the match operator.

>perl -E"@a = qw(a b); say @a ~~ 2 ? 'match' : 'no match'" no match >perl -E"@a = qw(a b); say @a ~~ (0+\@a) ? 'match' : 'no match'" match

The idea might be to allow the following

>perl -E"@a = qw(a b); say \@a ~~ \@a ? 'match' : 'no match'" match

but it actually uses a different rule.

>perl -E"@a = qw(a b); @b = qw(a b); say \@a ~~ \@b ? 'match' : 'no ma +tch'" match

This might be improvable. I'll see about bringing this up too.

Replies are listed 'Best First'.
Re^2: smart match operator should be smarter!
by 7stud (Deacon) on Nov 22, 2009 at 15:24 UTC

    Can you explain what those examples are supposed to demonstrate?

    What I got out of it is that the naive rule is:

    1) the match operator compares references.

    Therefore, if you smart match @arr ~~ 2, you are doing this:

    my @arr = (10, 20, 30); my $aref = \@arr; say $aref; #ARRAY(0x1810e90) 0x1810e90 ~~ 2 --output:-- NOPE!

    However, the naive rule breaks down here:

    my @a = (10, 20, 30); my @b = (10, 20, 30); my $aref = \@a; my $bref = \@b; say $aref; say $bref; --output:-- ARRAY(0x1810e90) ARRAY(0x181ace0) #yet... say @a ~~ @b ? 'yes' : 'no'; --output:-- yes

    So you can't really say that the smart operator is comparing references, otherwise @a and @b would not match.

      Like I said, it's a different rule (ARRAY ~~ ARRAY instead of ANY ~~ NUM).