blib/lib/Attribute/Overload/Match.pm

filter: Statementstmt 71.9% 41 / 57 Branchbran 47.0% 16 / 34 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 69.2% 9 / 13 Podpod 0.0% 0 / 3 totaltotal 58.9% 66 / 112 CC 21 SCAR 30.3
CC
21
Cov
61%
parse
49.1
__ANON__
12.0
__ANON__
6.0
CRAP 20.6
↑ Summary
1 # $Id: Match.pm,v 1.1.1.1 2007/02/28 11:49:47 dk Exp $
2
3 package Attribute::Overload::Match;
4
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Attribute::Handlers;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 our ( %ops, $VERSION);
9 1 $VERSION = '0.01';
10
11 sub handle
12 441 ▶{
Subroutine handle: called CC=5 SCAR=16.2
Pod undocumented
13 441 my ( $pkg, $op) = ( shift, shift );
14 441 NEXTARG: for my $arg ( @{$ops{$pkg}{$op}}) {
15 477 my $sym = $$arg[0];
16 477 ▶ next if $#$arg > @_;
Branch: 1 branch
BranchTrueFalseTotal
if $arg->$#* > @_ 0 477 477
17 477 ▶ for ( my $x = 1; $x < @$arg; $x++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < @$arg) 71 441 512
18 71 ▶ next NEXTARG unless $arg-> [$x]->( $_[$x - 1]);
Branch: 1 branch
BranchTrueFalseTotal
unless $arg->[$x]->($_[$x - 1]) 36 35 71
19 }
20 441 goto $sym;
21
22 }
23 0 die "Nothing matches $op in $pkg";
24
25 }
26
27 sub parse
28 11 ▶{
Subroutine parse: called CC=12 SCAR=38.9
Pod undocumented
29 11 my @r;
30 11 for my $v ( @_) {
31 2 $v =~ s/^\s*//;
32 2 $v =~ s/\s*$//;
33 2 ▶ if ( $v eq '') {
Branch: 7 branches
BranchTrueFalseTotal
if ($v eq '') { } 0 2 2
elsif ($v =~ /^\d/) { } 1 1 2
elsif ($v =~ /^'(.*)'$/) { } 0 1 1
elsif ($v =~ /^[A-Z]/) { } 0 1 1
elsif ($v eq '//') { } 0 1 1
elsif ($v =~ /^(<|>|lt|gt|eq|==)\s*(.*)$/) { } 1 0 1
elsif ($v =~ /^(ne|!=)\s*(.*)$/) { } 0 0 0
34 0 ▶ push @r, sub { 1 };
Subroutine __ANON__: not called CC=1 SCAR=6.9
35 } elsif ( $v =~ /^\d/) {
36 1 ▶ push @r, sub { defined $_[0] and $_[0] == $v };
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] and $_[0] == $v 4 0 4
37 } elsif ( $v =~ /^'(.*)'$/ ) {
38 0 $v = $1;
39 0 ▶ push @r, sub { defined $_[0] and $_[0] eq $v };
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] and $_[0] eq $v 0 0 0
40 } elsif ( $v =~ /^[A-Z]/) {
41 0 ▶ push @r, sub { defined $_[0] and ref($_[0]) and $_[0]->isa($v) };
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] and ref $_[0] and $_[0]->isa($v) 0 0 0
Condition0%0/3
defined $_[0] and ref $_[0]
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_[0] and ref $_[0]
defined $_[0] ref $_[0]
Truth table0%0/3
A and B
A= defined $_[0]B= ref $_[0]
ABresult
0X0
100
111
42 } elsif ( $v eq '//') {
43 0 ▶ push @r, sub { defined $_[0] };
Subroutine __ANON__: not called CC=1 SCAR=6.9
44 } elsif ( $v =~ /^(<|>|lt|gt|eq|==)\s*(.*)$/) {
45 1 $v = eval "sub { defined \$_[0] and \$_[0] $v ;}";
46 1 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
47 1 push @r, $v;
48 } elsif ( $v =~ /^(ne|!=)\s*(.*)$/) {
49 0 $v = eval "sub { not defined \$_[0] or \$_[0] $v ;}";
50 0 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
51 0 push @r, $v;
52 } else {
53 0 $v = eval "sub { $v }";
54 0 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
55 0 push @r, $v;
56 }
57 }
58 11 @r;
59 }
60
61 11 ▶sub UNIVERSAL::op : ATTR(CODE,RAWDATA) {
Subroutine op: called CC=2 SCAR=6.9
Pod undocumented
62 11 my ($pkg, $sub, $data) = @_[0,2,4];
63 11 require overload;
64 11 my ($op, @arg) = split( ',', $data);
65 441 ▶ overload::OVERLOAD( $pkg, $op, sub { handle( $pkg, $op, @_ ) } )
Subroutine __ANON__: called CC=1 SCAR=0.0
66 11 ▶ unless exists $ops{$pkg}{$op};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Attribute::Overload::Match::ops{$pkg}{$op} 8 3 11
67 11 push @{$ops{$pkg}{$op}}, [ $sub, parse @arg ];
68 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
69
70 1 1;
71
72 =pod
73
74 =head1 NAME
75
76 Attribute::Overload::Match - argument-dependent handlers for overloaded operators
77
78 =head1 DESCRIPTION
79
80 The module is a wrapper for L<overload>, that provides a simple syntax for
81 calling different operator handlers for different passed arguments. The idea is
82 a curious ( but probably not a very practical ) mix of L<Attribute::Overload>
83 and L<Sub::PatMat> .
84
85 =head1 SYNOPSIS
86
87 use Attribute::Overload::Match;
88
89 Suppose we declare a class that overloads operations on integers:
90
91 sub new($) { my $x = $_[0]; bless \$x, __PACKAGE__ }
92 sub val($) { ${$_[0]} }
93 sub eq : op(==) { val(shift) == shift }
94 sub subtract : op(-) { new val(shift) - shift }
95 sub mul : op(*) { new val(shift) * shift }
96 sub add : op(+) { new val(shift) + shift }
97 sub qq : op("") { val(shift) }
98 sub le : op(<) { val(shift) < shift }
99 ...
100
101 then we can change meaning of some operators with a touch of functional style:
102
103 no warnings 'redefine';
104 sub fac : op(!,1) { new 1 }
105 sub fac : op(!) { !($_[0] - 1) * $_[0] }
106
107 or
108
109 sub fib : op(~,<2) { new 1 }
110 sub fib : op(~) { ~( $_[0] - 1) + ~($_[0] - 2) }
111
112 (if you don't like C<no warnings 'redefine'>, just use different sub names for C<fac> etc)
113 thus
114
115 my $x = !new(10);
116 print "$x\n";
117 3628800
118
119 and
120
121 my $x = ~new(10);
122 print "$x\n";
123 89
124
125 =head1 SYNTAX
126
127 The only syntax available here is syntax that is passed to C<op> attributes,
128 which is in general C<sub mysub : op(OPERATOR,CODE[,CODE[,CODE ...]])>, where
129 C<OPERATOR> belongs to strings defined in L<overload> ( such as C<+>, C<[]>,
130 C<""> etc), and C<CODE> strings are perl code, matching a parameter. However,
131 for the sake of readability, C<CODE> can be also one of the following
132 signatures:
133
134 =over
135
136 =item Empty string
137
138 Parameter is never checked
139
140 =item String starting with a digit
141
142 Pataremeter must be defined and be equal (C<==>) to the value if the string
143
144 =item Single-quoted string
145
146 Parameter must be defined and be equal (C<eq>) to the value if the string
147
148 =item Non-quoted string beginning with a capital letter
149
150 The string defined as a class name. Parameter must be defined and be an instance
151 of the class (or its descendant).
152
153 =item C<//>
154
155 Parameter must be defined.
156
157 =item One of C<< <,>,lt,gt,eq,==,ne,!= >> followed by an expression
158
159 Parameter must be defined and return true when compared with the expression
160 using given comparison operator
161
162 =item Anything else
163
164 Anything else is passed directly to C<eval> and is treated in a boolean context
165 thereafter.
166
167 =back
168
169 =head1 ACKNOWLEDGEMENTS
170
171 Thanks to Anton Berezin for ideas on L<Sub::PatMat> .
172 Thanks to H. Merijn Brandt for C<//>.
173
174 =head1 SEE ALSO
175
176 L<Attribute::Overload>, L<Sub::PatMat>, L<overload>.
177
178 =head1 COPYRIGHT
179
180 This library is free software; you can redistribute it and/or modify it
181 under the same terms as Perl itself.
182
183
184 =head1 AUTHOR
185
186 Dmitry Karasik <dmitry@karasik.eu.org>
187
188 =cut
↑ Summary