From: Matt S Trout Date: Sun, 6 Jun 2010 01:38:13 +0000 (+0100) Subject: unary operators X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=c3633a0ebac9f306a274f26b186f8cce391b0ed8;p=dbsrgits%2FData-Query.git unary operators --- diff --git a/lib/Data/Query/ExprBuilder.pm b/lib/Data/Query/ExprBuilder.pm index 9e12dd6..3238a28 100644 --- a/lib/Data/Query/ExprBuilder.pm +++ b/lib/Data/Query/ExprBuilder.pm @@ -5,6 +5,19 @@ use Data::Query::Constants qw(DQ_OPERATOR DQ_VALUE); use Scalar::Util (); use overload ( + # unary operators + (map { + my $op = $_; + $op => sub { + Data::Query::ExprBuilder->new({ + expr => { + type => DQ_OPERATOR, + operator => { perl => $op }, + args => [ $_[0]->{expr} ] + } + }); + } + } qw(! neg)), # binary operators (map { my ($overload, $as) = ref($_) ? @$_ : ($_, $_); @@ -23,6 +36,7 @@ use overload ( subtype => { perl => 'Scalar' }, value => $_ } + # we're called with ($left, $right, 0) or ($right, $left, 1) } $_[2] ? @_[1,0] : @_[0,1] ] }, diff --git a/t/expr.t b/t/expr.t index 590cc80..fed19d7 100644 --- a/t/expr.t +++ b/t/expr.t @@ -108,3 +108,11 @@ expr_is { $_->foo | $_->bar } ], }, 'Masquerade for | as or ok'; + +expr_is { !$_->foo } + { + type => DQ_OPERATOR, + operator => { perl => '!' }, + args => [ expr { $_->foo } ], + }, + '! ok';