blib/lib/Regexp/Bind.pm

filter: Statementstmt 98.7% 80 / 81 Branchbran 93.3% 28 / 30 Conditioncond 50.0% 8 / 16 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 4 totaltotal 84.6% 127 / 150 CC 25 SCAR 17.5
CC
25
Cov
91%
bind
9.1
global_bind
8.1
global_bind_array
5.0
CRAP 5.7
↑ Summary
1 =pod
2  
3 =head1 NAME
4  
5 Regexp::Bind - Bind variables to captured buffers
6  
7 =head1 SYNOPSIS
8  
9 use Regexp::Bind qw(
10 bind global_bind
11 bind_array global_bind_array
12 );
13  
14 $record = bind($string, $regexp, @fields);
15 @record = global_bind($string, $regexp, @fields);
16  
17 $record = bind(\$string, $regexp, @fields);
18 @record = global_bind(\$string, $regexp, @fields);
19  
20 $record = bind_array($string, $regexp);
21 @record = global_bind_array($string, $regexp);
22  
23 $record = bind_array(\$string, $regexp);
24 @record = global_bind_array(\$string, $regexp);
25  
26 $record = bind(\$string, $embedded_regexp);
27 @record = global_bind(\$string, $embedded_egexp);
28  
29  
30 =head1 DESCRIPTION
31  
32 This module is an extension to perl's native regexp function. It binds
33 anonymous hashes or named variables to matched buffers. Both normal
34 regexp syntax and embedded regexp syntax are supported. You can view
35 it as a tiny and petite data extraction system.
36  
37 =head1 FUNCTIONS
38  
39 Two types of function are exported. They bind the given fields to
40 captured contents, and return anonymous hashes/arrayes of the fields.
41  
42 In the following example, you can pass in either a string or a
43 string-reference.
44  
45 =head2 Match the first occurrence
46  
47 use Data::Dumper;
48  
49 =head3 Binding to anonymous hash
50  
51 $record = bind($string, $regexp, qw(field_1 field_2 field_3));
52 print Dumper $record;
53  
54 =head3 Binding to array
55  
56 $record = bind_array($string, $regexp);
57 print $record->[0];
58  
59 =head2 Do global matching and store matched parts in @record
60  
61 =head3 Binding to anonymous hash
62  
63 @record = global_bind($string, $regexp, qw(field_1 field_2 field_3));
64 print Dumper $_ foreach @record;
65  
66 =head3 Binding to array
67  
68 @record = global_bind_array($string, $regexp);
69 print $record[0]->[0];
70  
71 =head1 NAMED VARIABLE BINDING
72  
73 To use named variable binding, please set $Regexp::Bind::USE_NAMED_VAR to non-undef, and then matched parts will be bound to named variables while using bind(). It is not supported for global_bind(), bind_array() and global_bind_array().
74  
75 $Regexp::Bind::USE_NAMED_VAR = 1;
76 bind($string, $regexp, qw(field_1 field_2 field_3));
77 print "$field_1 $field_2 $field_3\n";
78  
79  
80 =head1 EMBEDDED REGEXP
81  
82 Using embedded regexp syntax means you can embed fields right in
83 regexp itself. Its embedded syntax exploits the feature of in-line
84 commenting in regexps.
85  
86 The module first tries to detect if embedded syntax is used. If
87 detected, then comments are stripped and regexp is turned back into a
88 simple one.
89  
90 Using embedded syntax, for the sake of simplicity and legibility,
91 field's name is restricted to B<alphanumerics> only. bind_array() and
92 global_bind_array() do not support embedded syntax.
93  
94  
95 Example:
96  
97 bind($string, qr'# (?#<field_1>\w+) (?#<field_2>\d+)\n'm);
98  
99 is converted into
100  
101 bind($string, qr'# (\w+) (\d+)\n'm);
102  
103 If embedded syntax is detected, further input arguments are ignored. It means that
104  
105 bind($string, qr'# (?#<field_1>\w+) (?#<field_2>\d+)\n'm,
106 qw(field_1 field_2));
107  
108 is the same as
109  
110 bind($string, qr'# (?#<field_1>\w+) (?#<field_2>\d+)\n'm);
111  
112 and conceptually equal to
113  
114 bind($string, qr'# (\w+) (\d+)\n'm, qw(field_1 field_2));
115  
116  
117  
118 Note that the module simply replaces B<(?#E<lt>field nameE<gt>> with
119 B<(> and binds the field's name to buffer. It does not check for
120 syntax correctness, so any fancier usage may crash.
121  
122  
123 =head1 INLINE FILTERING
124  
125 Inline filtering now works with B<embedded syntax>. Matched parts are
126 saved in $_, and you can do some simple transformation within the
127 brackets before they are exported.
128  
129 bind($string, qr'# (?#<field_1>{ s/\s+//, $_ }\w+) (?#<field_2>{ $_*= 10, $_ }\d+)\n'm);
130  
131  
132 =cut
133  
134 package Regexp::Bind;
135  
136 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
137 1 our @ISA = qw(Exporter);
138 1 our @EXPORT_OK = qw(bind global_bind bind_array global_bind_array);
139 1 our $VERSION = '0.05';
140  
141 1 our $USE_NAMED_VAR = 0;
142 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
143 1 ▶no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
144  
145 16 ▶sub _get_fields {
Subroutine _get_fields: called CC=2 SCAR=6.9
146 16 my @field;
147 16 ▶ while($_[0] =~ s,\(\?#<(\w+?)>,(,o){
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ s/\(\?#<(\w+?)>/(/o) 30 16 46
148 30 push @field, $1;
149 }
150 16 @field;
151 }
152  
153 16 ▶sub _get_filters {
Subroutine _get_filters: called CC=2 SCAR=6.9
154 16 my @filter;
155 # well, i know, this vulgar pattern doesn't really work for all occasions
156 # i will introduce Text::Balanced with this.
157 16 ▶ while($_[0] =~ s,(\(\?#(?:<(?:\w+?)>))\{(.+?)\},$1,o){
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ s/(\(\?#(?:<(?:\w+?)>))\{(.+?)\}/$1/o) 8 16 24
158 8 push @filter, $2;
159 }
160 16 (undef, map{eval 'sub { local $_ = shift;'.$_.'};' }@filter);
161 }
162  
163 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
164 1 ▶use B::Deparse;
Subroutine BEGIN: called CC=1 SCAR=0.0
165 10 ▶sub bind {
Subroutine bind: called CC=9 SCAR=22.1
Pod undocumented
166 10 ▶ my $string = (ref($_[0]) eq 'SCALAR' ? ${shift()} : shift) || die "No string input";
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'SCALAR' ? : 5 5 10
Condition50%1/2
(ref $_[0] eq 'SCALAR' ? ${shift();} : shift()) || die('No string input')
l!l
100
MC/DC0%0/1
(ref $_[0] eq 'SCALAR' ? ${shift();} : shift()) || die('No string input')
(ref $_[0] eq 'SCALAR' ? ${shift();} : shift())
Truth table50%1/2
A
A= (ref $_[0] eq 'SCALAR' ? ${shift();} : shift())
Aresult
11
00
167 10 ▶ my $regexp = shift || die "No regexp input";
Condition50%1/2
shift() || die('No regexp input')
l!l
100
MC/DC0%0/1
shift() || die('No regexp input')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
168  
169 10 my @filter = _get_filters $regexp;
170 10 my @field = _get_fields $regexp;
171 10 ▶ @field = @_ unless @field;
Branch: 1 branch
BranchTrueFalseTotal
unless @field 4 6 10
172  
173  
174 10 $string =~ m/$regexp/;
175 10 my $cnt = 1;
176 10 ▶ if($USE_NAMED_VAR){
Branch: 1 branch
BranchTrueFalseTotal
if ($Regexp::Bind::USE_NAMED_VAR) { } 4 6 10
177 4 my $pkg = (caller)[0];
178 4 foreach my $field (@field){
179 0 ▶ my $t = ref($filter[$cnt]) eq 'CODE'? $filter[$cnt]->(${$cnt}) : ${$cnt};
Branch: 1 branch
BranchTrueFalseTotal
ref $filter[$cnt] eq 'CODE' ? : 0 12 12
180 12 $cnt++;
181 12 ${"${pkg}::$field"} = $t;
182 }
183 }
184 else {
185 +{
186 map{
187 6 ▶ my $t = ref($filter[$cnt]) eq 'CODE'? $filter[$cnt]->(${$cnt}) : ${$cnt};
Branch: 1 branch
BranchTrueFalseTotal
ref $filter[$cnt] eq 'CODE' ? : 4 14 18
188 18 $cnt++;
189 18 $_ => $t;
190 } @field
191 };
192 }
193 }
194  
195 2 ▶sub bind_array {
Subroutine bind_array: called CC=4 SCAR=14.1
Pod undocumented
196 2 ▶ my $string = (ref($_[0]) eq 'SCALAR' ? ${shift()} : shift) || die "No string input";
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'SCALAR' ? : 1 1 2
Condition50%1/2
(ref $_[0] eq 'SCALAR' ? ${shift();} : shift()) || die('No string input')
l!l
20
MC/DC0%0/1
(ref $_[0] eq 'SCALAR' ? ${shift();} : shift()) || die('No string input')
(ref $_[0] eq 'SCALAR' ? ${shift();} : shift())
Truth table50%1/2
A
A= (ref $_[0] eq 'SCALAR' ? ${shift();} : shift())
Aresult
11
00
197 2 ▶ my $regexp = shift || die "No regexp input";
Condition50%1/2
shift() || die('No regexp input')
l!l
20
MC/DC0%0/1
shift() || die('No regexp input')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
198 2 my $cnt = 1;
199 2 [ ($string =~ m/$regexp/) ];
200 }
201  
202  
203 6 ▶sub global_bind {
Subroutine global_bind: called CC=8 SCAR=20.9
Pod undocumented
204 6 ▶ my $string = (ref($_[0]) eq 'SCALAR' ? ${shift()} : shift) || die "No string input";
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'SCALAR' ? : 3 3 6
Condition50%1/2
(ref $_[0] eq 'SCALAR' ? ${shift();} : shift()) || die('No string input')
l!l
60
MC/DC0%0/1
(ref $_[0] eq 'SCALAR' ? ${shift();} : shift()) || die('No string input')
(ref $_[0] eq 'SCALAR' ? ${shift();} : shift())
Truth table50%1/2
A
A= (ref $_[0] eq 'SCALAR' ? ${shift();} : shift())
Aresult
11
00
205 6 ▶ my $regexp = shift || die "No regexp input";
Condition50%1/2
shift() || die('No regexp input')
l!l
60
MC/DC0%0/1
shift() || die('No regexp input')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
206  
207 6 my @filter = _get_filters $regexp;
208 6 my @field = _get_fields $regexp;
209 6 ▶ @field = @_ unless @field;
Branch: 1 branch
BranchTrueFalseTotal
unless @field 2 4 6
210  
211 6 my @bind;
212 my $cnt;
213 6 ▶ while($string =~ m/$regexp/g){
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /$regexp/g) 18 6 24
214 18 $cnt = 1;
215 push @bind,
216 +{
217 map{
218 18 my $t = ref($filter[$cnt]) eq 'CODE'
219 54 ▶ ? $filter[$cnt]->(${$cnt}) : ${$cnt};
Branch: 1 branch
BranchTrueFalseTotal
ref $filter[$cnt] eq 'CODE' ? : 12 42 54
220 54 $cnt++;
221 54 $_ => $t;
222 } @field
223 };
224 }
225 6 ▶ wantarray ? @bind : \@bind;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 6 0 6
226 }
227  
228 2 ▶sub global_bind_array {
Subroutine global_bind_array: called CC=5 SCAR=16.2
Pod undocumented
229 2 ▶ my $string = (ref($_[0]) eq 'SCALAR' ? ${shift()} : shift) || die "No string input";
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'SCALAR' ? : 1 1 2
Condition50%1/2
(ref $_[0] eq 'SCALAR' ? ${shift();} : shift()) || die('No string input')
l!l
20
MC/DC0%0/1
(ref $_[0] eq 'SCALAR' ? ${shift();} : shift()) || die('No string input')
(ref $_[0] eq 'SCALAR' ? ${shift();} : shift())
Truth table50%1/2
A
A= (ref $_[0] eq 'SCALAR' ? ${shift();} : shift())
Aresult
11
00
230 2 ▶ my $regexp = shift || die "No regexp input";
Condition50%1/2
shift() || die('No regexp input')
l!l
20
MC/DC0%0/1
shift() || die('No regexp input')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
231  
232 2 my @bind;
233 2 ▶ push @bind, [ map{${$_}} 1..$#+ ] while $string =~ m/$regexp/g;
Branch: 1 branch
BranchTrueFalseTotal
$string =~ /$regexp/g and do { push @bind, [map({${$_;};} 1 .. $#+)] } 6 2 8
234 2 @bind;
235 }
236  
237 1 1;
238 __END__
↑ Summary