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 | |
by ikegami (Patriarch) on Nov 22, 2009 at 18:12 UTC |