add & and | overloading to and and or
[dbsrgits/Data-Query.git] / lib / Data / Query / ExprBuilder.pm
CommitLineData
9ee33178 1package Data::Query::ExprBuilder;
2
3use strictures 1;
b616bc41 4use Data::Query::Constants qw(DQ_OPERATOR DQ_VALUE);
5use Scalar::Util ();
6
7use overload (
7e599929 8 # binary operators
b616bc41 9 (map {
7e599929 10 my ($overload, $as) = ref($_) ? @$_ : ($_, $_);
11 $overload => sub {
b616bc41 12 Data::Query::ExprBuilder->new({
13 expr => {
14 type => DQ_OPERATOR,
7e599929 15 operator => { perl => $as },
b616bc41 16 args => [
17 map {
18 (Scalar::Util::blessed($_)
19 && $_->isa('Data::Query::ExprBuilder'))
20 ? $_->{expr}
21 : {
22 type => DQ_VALUE,
23 subtype => { perl => 'Scalar' },
24 value => $_
25 }
26 } $_[2] ? @_[1,0] : @_[0,1]
27 ]
28 },
29 });
30 }
7e599929 31 }
32 qw(+ - * / % ** << >> . < > == != lt le gt ge eq ne),
33
34 # since 'and' and 'or' aren't operators we borrow the bitwise ops
35 [ '&' => 'and' ], [ '|' => 'or' ],
36 ),
37 # unsupported
9e0200bc 38 (map {
39 my $op = $_;
40 $op => sub { die "Can't use operator $op on a ".ref($_[0]) }
7e599929 41 } qw(<=> cmp x ^ ~)
9e0200bc 42 ),
b616bc41 43);
9ee33178 44
45sub new {
46 bless({ %{$_[1]} }, (ref($_[0])||$_[0]));
47}
48
491;