Skip to content

Commit

Permalink
clean up the rjbs engine and fix up some undef warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
doy committed Jul 8, 2011
1 parent 5649f76 commit 635f02a
Showing 1 changed file with 38 additions and 7 deletions.
45 changes: 38 additions & 7 deletions lib/smartmatch/engine/rjbs.pm
Expand Up @@ -5,25 +5,56 @@ 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, '=~')) {
return 'Regex';
}
else {
return 'unknown object';
}
}
elsif (reftype($thing) eq 'CODE') {
return 'Code';
}
else {
return 'unknown';
}
}

sub match {
my ($a, $b) = @_;

if (!defined($b)) {
if (type($b) eq 'undef') {
return !defined($a);
}
elsif (blessed($b) && (my $overload = overload::Method($b, '~~'))) {
elsif (type($b) eq 'Overloaded') {
my $overload = overload::Method($b, '~~');
return $b->$overload($a, 1);
}
elsif (reftype($b) eq 'REGEXP') {
return $a =~ $b;
}
elsif (blessed($b) && overload::Method($b, '=~')) {
elsif (type($b) eq 'Regex') {
return $a =~ $b;
}
elsif (!blessed($b) && reftype($b) eq 'CODE') {
elsif (type($b) eq 'Code') {
return $b->($a);
}
else {
$a //= 'undef';
$b //= 'undef';
die "invalid smart match: $a ~~ $b";
}
}
Expand Down

0 comments on commit 635f02a

Please sign in to comment.