use Scalar::Util ();
use overload (
+ # binary operators
(map {
- my $op = $_;
- $op => sub {
+ my ($overload, $as) = ref($_) ? @$_ : ($_, $_);
+ $overload => sub {
Data::Query::ExprBuilder->new({
expr => {
type => DQ_OPERATOR,
- operator => { perl => $op },
+ operator => { perl => $as },
args => [
map {
(Scalar::Util::blessed($_)
},
});
}
- } qw(+ - * / % ** << >> . < > == != lt le gt ge eq ne)),
+ }
+ qw(+ - * / % ** << >> . < > == != lt le gt ge eq ne),
+
+ # since 'and' and 'or' aren't operators we borrow the bitwise ops
+ [ '&' => 'and' ], [ '|' => 'or' ],
+ ),
+ # unsupported
(map {
my $op = $_;
$op => sub { die "Can't use operator $op on a ".ref($_[0]) }
- } qw(<=> cmp x)
+ } qw(<=> cmp x ^ ~)
),
);
throws_ok {
expr { $_->foo <=> 3 }
} qr/\QCan't use operator <=>/, 'Exception on bad operator';
+
+expr_is { $_->foo & $_->bar }
+ {
+ type => DQ_OPERATOR,
+ operator => { perl => 'and' },
+ args => [
+ expr { $_->foo },
+ expr { $_->bar },
+ ],
+ },
+ 'Masquerade for & as and ok';
+
+expr_is { $_->foo | $_->bar }
+ {
+ type => DQ_OPERATOR,
+ operator => { perl => 'or' },
+ args => [
+ expr { $_->foo },
+ expr { $_->bar },
+ ],
+ },
+ 'Masquerade for | as or ok';