Subject: | Update to true perl 6/5.10 smart matching |
Hello,
Is there any chance of seeing this updated to match the actual perl 6
spec, which has changed since Apocalypse 4?
Some of the differences seem to be:
module executes
Code[+] Code[+] referential equality $a == $b
modules is missing exists
Hash Array hash slice existence grep {exists $a->{$_}} @$b
module recurses
Hash Any hash entry existence exists $a->{$b}
module greps values??
Array Array arrays are identical[*]
module recurses?
Array Regex array grep grep /$b/, @$a
Array Num array contains number grep $_ == $b, @$a
Array Any array contains string grep $_ eq $b, @$a
The current implementation yields the following results against a
modified version of 5.10.0's smartmatch.t:
1..144
ok 1 - smart_match(\&foo, \&foo): 1
ok 2 - smart_match(\&foo, \&foo): 1
ok 3 - smart_match(\&foo, sub {}):
ok 4 - smart_match(sub {}, \&foo):
ok 5 - smart_match(\&foo, \&bar):
not ok 6 - smart_match(\&bar, \&foo): 2
# Failed at smartmatch.t line 56
ok 7 - smart_match(1, sub{shift}): 1
ok 8 - smart_match(sub{shift}, 1): 1
ok 9 - smart_match(0, sub{shift}):
ok 10 - smart_match(sub{shift}, 0):
ok 11 - smart_match(1, sub{scalar @_}): 1
ok 12 - smart_match(sub{scalar @_}, 1): 1
ok 13 - smart_match([], \&bar): 2
ok 14 - smart_match(\&bar, []): 2
ok 15 - smart_match({}, \&bar): 2
ok 16 - smart_match(\&bar, {}): 2
ok 17 - smart_match(qr//, \&bar): 2
ok 18 - smart_match(\&bar, qr//): 2
ok 19 - smart_match(a_const, "a constant"): 1
ok 20 - smart_match("a constant", a_const): 1
ok 21 - smart_match(a_const, a_const): 1
ok 22 - smart_match(a_const, a_const): 1
ok 23 - smart_match(a_const, b_const): 1
ok 24 - smart_match(b_const, a_const): 1
not ok 25 - smart_match({}, {}):
# Failed at smartmatch.t line 56
not ok 26 - smart_match({}, {}):
# Failed at smartmatch.t line 56
ok 27 - smart_match({}, {1 => 2}):
ok 28 - smart_match({1 => 2}, {}):
ok 29 - smart_match({1 => 2}, {1 => 2}): 1
ok 30 - smart_match({1 => 2}, {1 => 2}): 1
ok 31 - smart_match({1 => 2}, {1 => 3}): 1
ok 32 - smart_match({1 => 3}, {1 => 2}): 1
ok 33 - smart_match({1 => 2}, {2 => 3}):
ok 34 - smart_match({2 => 3}, {1 => 2}):
ok 35 - smart_match(\%main::, {map {$_ => 'x'} keys %main::}): 1
ok 36 - smart_match({map {$_ => 'x'} keys %main::}, \%main::): 1
ok 37 - smart_match(\%hash, \%tied_hash): 1
ok 38 - smart_match(\%tied_hash, \%hash): 1
ok 39 - smart_match(\%tied_hash, \%tied_hash): 1
ok 40 - smart_match(\%tied_hash, \%tied_hash): 1
ok 41 - smart_match(\%::, [keys %main::]): 1
ok 42 - smart_match([keys %main::], \%::): 1
ok 43 - smart_match(\%::, []):
ok 44 - smart_match([], \%::):
Use of uninitialized value in hash element at
/usr/lib/perl5/site_perl/5.8.8/Match/Smart.pm line 65, <DATA> line 113.
ok 45 - smart_match({"" => 1}, [undef]): 1
Use of uninitialized value in hash element at
/usr/lib/perl5/site_perl/5.8.8/Match/Smart.pm line 65, <DATA> line 113.
ok 46 - smart_match([undef], {"" => 1}): 1
ok 47 - smart_match({ foo => 1 }, ["foo"]): 1
ok 48 - smart_match(["foo"], { foo => 1 }): 1
ok 49 - smart_match({ foo => 1 }, ["foo", "bar"]): 1
ok 50 - smart_match(["foo", "bar"], { foo => 1 }): 1
ok 51 - smart_match(\%hash, ["foo", "bar"]): 1
ok 52 - smart_match(["foo", "bar"], \%hash): 1
ok 53 - smart_match(\%hash, ["foo"]): 1
ok 54 - smart_match(["foo"], \%hash): 1
ok 55 - smart_match(\%hash, ["quux"]):
ok 56 - smart_match(["quux"], \%hash):
ok 57 - smart_match(\%hash, [qw(foo quux)]): 1
ok 58 - smart_match([qw(foo quux)], \%hash): 1
ok 59 - smart_match({foo => 1}, qr/^(fo[ox])$/): 1
ok 60 - smart_match(qr/^(fo[ox])$/, {foo => 1}): 1
ok 61 - smart_match(+{0..100}, qr/[13579]$/):
ok 62 - smart_match(qr/[13579]$/, +{0..100}):
ok 63 - smart_match(+{foo => 1, bar => 2}, "foo"): 1
ok 64 - smart_match("foo", +{foo => 1, bar => 2}): 1
ok 65 - smart_match(+{foo => 1, bar => 2}, "baz"):
ok 66 - smart_match("baz", +{foo => 1, bar => 2}):
not ok 67 - smart_match([], []):
# Failed at smartmatch.t line 56
not ok 68 - smart_match([], []):
# Failed at smartmatch.t line 56
ok 69 - smart_match([], [1]):
ok 70 - smart_match([1], []):
ok 71 - smart_match([["foo"], ["bar"]], [qr/o/, qr/a/]): 1
ok 72 - smart_match([qr/o/, qr/a/], [["foo"], ["bar"]]): 1
ok 73 - smart_match(["foo", "bar"], [qr/o/, qr/a/]): 1
ok 74 - smart_match([qr/o/, qr/a/], ["foo", "bar"]): 1
not ok 75 - smart_match(["foo", "bar"], [qr/o/, "foo"]): 1
# Failed at smartmatch.t line 56
not ok 76 - smart_match([qr/o/, "foo"], ["foo", "bar"]): 1
# Failed at smartmatch.t line 56
ok 77 - smart_match($deep1, $deep1): 1
ok 78 - smart_match($deep1, $deep1): 1
ok 79 - smart_match($deep1, $deep2):
ok 80 - smart_match($deep2, $deep1):
ok 81 - smart_match(\@nums, \@tied_nums): 1
ok 82 - smart_match(\@tied_nums, \@nums): 1
ok 83 - smart_match([qw(foo bar baz quux)], qr/x/): 1
ok 84 - smart_match(qr/x/, [qw(foo bar baz quux)]): 1
ok 85 - smart_match([qw(foo bar baz quux)], qr/y/):
ok 86 - smart_match(qr/y/, [qw(foo bar baz quux)]):
not ok 87 - smart_match([qw(1foo 2bar)], 2):
# Failed at smartmatch.t line 56
not ok 88 - smart_match(2, [qw(1foo 2bar)]):
# Failed at smartmatch.t line 56
ok 89 - smart_match([qw(1foo 2bar)], "2"):
ok 90 - smart_match("2", [qw(1foo 2bar)]):
ok 91 - smart_match(2, 2): 1
ok 92 - smart_match(2, 2): 1
ok 93 - smart_match(2, 3):
ok 94 - smart_match(3, 2):
ok 95 - smart_match(2, "2"): 1
ok 96 - smart_match("2", 2): 1
ok 97 - smart_match(2, "2.0"): 1
ok 98 - smart_match("2.0", 2): 1
not ok 99 - smart_match(2, "2bananas"): 1
# Failed at smartmatch.t line 56
not ok 100 - smart_match("2bananas", 2): 1
# Failed at smartmatch.t line 56
ok 101 - smart_match(2_3, "2_3"):
ok 102 - smart_match("2_3", 2_3):
ok 103 - smart_match(qr/x/, "x"): 1
ok 104 - smart_match("x", qr/x/): 1
ok 105 - smart_match(qr/y/, "x"):
ok 106 - smart_match("x", qr/y/):
ok 107 - smart_match(12345, qr/3/): 1
ok 108 - smart_match(qr/3/, 12345): 1
not ok 109 - smart_match(@nums, 7):
# Failed at smartmatch.t line 56
not ok 110 - smart_match(7, @nums):
# Failed at smartmatch.t line 56
not ok 111 - smart_match(@nums, \@nums):
# Failed at smartmatch.t line 56
ok 112 - smart_match(\@nums, @nums): 2
ok 113 - smart_match(@nums, \\@nums):
not ok 114 - smart_match(\\@nums, @nums): 2
# Failed at smartmatch.t line 56
not ok 115 - smart_match(@nums, [1..10]):
# Failed at smartmatch.t line 56
ok 116 - smart_match([1..10], @nums): 2
ok 117 - smart_match(@nums, [0..9]):
not ok 118 - smart_match([0..9], @nums): 1
# Failed at smartmatch.t line 56
not ok 119 - smart_match(%hash, "foo"):
# Failed at smartmatch.t line 56
not ok 120 - smart_match("foo", %hash):
# Failed at smartmatch.t line 56
not ok 121 - smart_match(%hash, /bar/):
# Failed at smartmatch.t line 56
not ok 122 - smart_match(/bar/, %hash):
# Failed at smartmatch.t line 56
not ok 123 - smart_match(%hash, [qw(bar)]):
# Failed at smartmatch.t line 56
ok 124 - smart_match([qw(bar)], %hash): 1
ok 125 - smart_match(%hash, [qw(a b c)]):
ok 126 - smart_match([qw(a b c)], %hash):
not ok 127 - smart_match(%hash, %hash):
# Failed at smartmatch.t line 56
not ok 128 - smart_match(%hash, %hash):
# Failed at smartmatch.t line 56
not ok 129 - smart_match(%hash, {%hash}):
# Failed at smartmatch.t line 56
ok 130 - smart_match({%hash}, %hash): 23
not ok 131 - smart_match(%hash, %tied_hash):
# Failed at smartmatch.t line 56
not ok 132 - smart_match(%tied_hash, %hash):
# Failed at smartmatch.t line 56
not ok 133 - smart_match(%tied_hash, %tied_hash):
# Failed at smartmatch.t line 56
not ok 134 - smart_match(%tied_hash, %tied_hash):
# Failed at smartmatch.t line 56
not ok 135 - smart_match(%hash, { foo => 5, bar => 10 }):
# Failed at smartmatch.t line 56
ok 136 - smart_match({ foo => 5, bar => 10 }, %hash): 10
ok 137 - smart_match(%hash, { foo => 5, bar => 10, quux => 15 }):
not ok 138 - smart_match({ foo => 5, bar => 10, quux => 15 }, %hash): 10
# Failed at smartmatch.t line 56
not ok 139 - smart_match(@nums, { 1, '', 2, '' }):
# Failed at smartmatch.t line 56
not ok 140 - smart_match({ 1, '', 2, '' }, @nums):
# Failed at smartmatch.t line 56
not ok 141 - smart_match(@nums, { 1, '', 12, '' }):
# Failed at smartmatch.t line 56
not ok 142 - smart_match({ 1, '', 12, '' }, @nums):
# Failed at smartmatch.t line 56
ok 143 - smart_match(@nums, { 11, '', 12, '' }):
ok 144 - smart_match({ 11, '', 12, '' }, @nums):
Subject: | smartmatch.patch |
--- /opt/src/perl-5.10.0/t/op/smartmatch.t 2007-12-18 05:47:08.000000000 -0500
+++ smartmatch.t 2009-12-09 22:54:31.000000000 -0500
@@ -1,8 +1,8 @@
#!./perl
BEGIN {
- chdir 't';
- @INC = '../lib';
+# chdir 't';
+# @INC = '../lib';
require './test.pl';
}
use strict;
@@ -10,6 +10,8 @@
use Tie::Array;
use Tie::Hash;
+use Match::Smart 'smart_match';
+
# The feature mechanism is tested in t/lib/feature/smartmatch:
# This file tests the semantics of the operator, without worrying
# about feature issues such as scoping etc.
@@ -45,10 +47,10 @@
die "Bad test spec: ($yn, $left, $right)"
unless $yn eq "" || $yn eq "!";
- my $tstr = "$left ~~ $right";
+ my $tstr = $] >= 5.009 ? "$left ~~ $right" : "smart_match($left, $right)";
my $res;
- $res = eval $tstr // ""; #/ <- fix syntax colouring
+ $res = eval $tstr || ""; #/ <- fix syntax colouring
die $@ if $@ ne "";
ok( ($yn =~ /!/ xor $res), "$tstr: $res");