summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/smartmatch/engine/rjbs.pm31
1 files changed, 31 insertions, 0 deletions
diff --git a/lib/smartmatch/engine/rjbs.pm b/lib/smartmatch/engine/rjbs.pm
new file mode 100644
index 0000000..fb2dfc6
--- /dev/null
+++ b/lib/smartmatch/engine/rjbs.pm
@@ -0,0 +1,31 @@
+package smartmatch::engine::rjbs;
+use strict;
+use warnings;
+
+use overload ();
+use Scalar::Util qw(blessed reftype);
+
+sub match {
+ my ($a, $b) = @_;
+
+ if (!defined($b)) {
+ return !defined($a);
+ }
+ elsif (blessed($b) && my $overload = overload::Method($b, '~~')) {
+ return $b->$overload($a, 1);
+ }
+ elsif (reftype($b) eq 'REGEXP') {
+ return $a =~ $b;
+ }
+ elsif (blessed($b) && my $overload = overload::Method($b, '=~')) {
+ return $a =~ $b;
+ }
+ elsif (!blessed($b) && reftype($b) eq 'CODE') {
+ return $b->($a);
+ }
+ else {
+ die "invalid smart match: $a ~~ $b";
+ }
+}
+
+1;