summaryrefslogtreecommitdiffstats
path: root/lib/smartmatch/engine/rjbs.pm
blob: 6f0d964e3eacaba8ea9650003177c8144815957c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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) && overload::Method($b, '=~')) {
        return $a =~ $b;
    }
    elsif (!blessed($b) && reftype($b) eq 'CODE') {
        return $b->($a);
    }
    else {
        die "invalid smart match: $a ~~ $b";
    }
}

1;