From 1a0c3c3a02da09abc11bbad291d09a41c1850d1d Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Fri, 8 Jul 2011 03:01:49 -0500 Subject: split this out into its own dist, and implement the custom opcode --- lib/smartmatch.pm | 39 --------------------------- lib/smartmatch/engine/core.pm | 15 +++++++++++ lib/smartmatch/engine/rjbs.pm | 62 ------------------------------------------- 3 files changed, 15 insertions(+), 101 deletions(-) delete mode 100644 lib/smartmatch.pm delete mode 100644 lib/smartmatch/engine/rjbs.pm (limited to 'lib') diff --git a/lib/smartmatch.pm b/lib/smartmatch.pm deleted file mode 100644 index 071e4ea..0000000 --- a/lib/smartmatch.pm +++ /dev/null @@ -1,39 +0,0 @@ -package smartmatch; -use strict; -use warnings; -use 5.010; -# ABSTRACT: pluggable smart matching backends - -use parent 'DynaLoader'; -use B::Hooks::OP::Check; - -sub dl_load_flags { 0x01 } - -__PACKAGE__->bootstrap( - # we need to be careful not to touch $VERSION at compile time, otherwise - # DynaLoader will assume it's set and check against it, which will cause - # fail when being run in the checkout without dzil having set the actual - # $VERSION - exists $smartmatch::{VERSION} - ? ${ $smartmatch::{VERSION} } : (), -); - -sub import { - my $package = shift; - my ($cb) = @_; - - if (!ref($cb)) { - my $engine = "smartmatch::engine::$cb"; - eval "require $engine; 1" - or die "Couldn't load smartmatch engine $engine: $@"; - $cb = $engine->can('match') unless ref($cb); - } - - smartmatch::register($cb); -} - -sub unimport { - smartmatch::unregister(); -} - -1; diff --git a/lib/smartmatch/engine/core.pm b/lib/smartmatch/engine/core.pm index 410c983..948152f 100644 --- a/lib/smartmatch/engine/core.pm +++ b/lib/smartmatch/engine/core.pm @@ -2,6 +2,21 @@ package smartmatch::engine::core; use strict; use warnings; use 5.010; +# ABSTRACT: default smartmatch implementation from 5.10 - 5.14 + +use parent 'DynaLoader'; + +sub dl_load_flags { 0x01 } + +__PACKAGE__->bootstrap( + # we need to be careful not to touch $VERSION at compile time, otherwise + # DynaLoader will assume it's set and check against it, which will cause + # fail when being run in the checkout without dzil having set the actual + # $VERSION + exists $smartmatch::engine::core::{VERSION} + ? ${ $smartmatch::engine::core::{VERSION} } : (), +); +smartmatch::engine::core::init(__PACKAGE__->can('match')); use B; use Carp qw(croak); diff --git a/lib/smartmatch/engine/rjbs.pm b/lib/smartmatch/engine/rjbs.pm deleted file mode 100644 index da40998..0000000 --- a/lib/smartmatch/engine/rjbs.pm +++ /dev/null @@ -1,62 +0,0 @@ -package smartmatch::engine::rjbs; -use strict; -use warnings; - -use overload (); -use Scalar::Util qw(blessed reftype); - -sub type { - my ($thing) = @_; - - if (!defined($thing)) { - return 'undef'; - } - elsif (!ref($thing)) { - return 'unknown non-ref'; - } - elsif (reftype($thing) eq 'REGEXP') { - return 'Regex'; - } - elsif (blessed($thing)) { - if (overload::Method($thing, '~~')) { - return 'Overloaded'; - } - elsif (overload::Method($thing, 'qr')) { - return 'Regex'; - } - else { - return 'unknown object'; - } - } - elsif (reftype($thing) eq 'CODE') { - return 'Code'; - } - else { - return 'unknown'; - } -} - -sub match { - my ($a, $b) = @_; - - if (type($b) eq 'undef') { - return !defined($a); - } - elsif (type($b) eq 'Overloaded') { - my $overload = overload::Method($b, '~~'); - return $b->$overload($a, 1); - } - elsif (type($b) eq 'Regex') { - return $a =~ $b; - } - elsif (type($b) eq 'Code') { - return $b->($a); - } - else { - $a //= 'undef'; - $b //= 'undef'; - die "invalid smart match: $a ~~ $b"; - } -} - -1; -- cgit v1.2.3-54-g00ecf