blib/lib/Regexp/Common/WithActions.pm untested

filter: Statementstmt 0.0% 0 / 99 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 168 CC 18 SCAR 34.1
CC
18
Cov
0%
gen_dequoter
56.0
gen_quoter
42.0
action
30.0
CRAP 30.1
1 use 5.008;
2 use strict;
3 use warnings;
4  
5 package Regexp::Common::WithActions;
6  
7 our $VERSION = '0.01';
8  
9 =head1 NAME
10  
11 Regexp::Common::WithActions - adds actions to Regexp::Common
12  
13 =head1 SYNOPSIS
14  
15 use Regexp::Common::WithActions;
16 my $quoted = $RE{quoted}->action('quote')->(q{a string with ' or "});
17 my $dequoted = $RE{quoted}->action('dequote')->(q{'a string with \' or "'});
18  
19 =head1 DESCRIPTION
20  
21 Some regular expressions from L<Regexp::Common> may be much better with
22 actions to manipulate matched data, for example for all variants
23 L<delimited|Regexp::Common::delimited> provides it's good to have quoter
24 and de-quoter actions.
25  
26 This module extends %RE with action method. It can be used in the same
27 way as subs or match methods. For example:
28  
29 $RE{some}{re}{-with => 'arguments'}->action('action')->('do something');
30  
31 As you can see action method returns a reference to a function implementing
32 particular action.
33  
34 =head1 CAVEAT
35  
36 Regexp::Common 2.122 has a problem that makes this module less useable. You
37 must load Regexp::Common::WithActions as the last thing (after all other
38 modules that can load R::C) in you programm.
39  
40 Patch for this issue exists and waiting for abigail to release a new version.
41  
42 =head1 ACTIONS
43  
44 =head2 delimited and quoted
45  
46 'quote' and 'dequote' are two actions provided for these regexps. Both work
47 in place in void context and return new value in other cases.
48  
49 =head2 more
50  
51 It's very easy to add a new action for other regexps in the module. Patches
52 are welcome.
53  
54 =cut
55  
56 our @ISA;
57  
58 our %ACTION = (
59 static => {},
60 generated => { },
61 );
62  
63 use Regexp::Common;
64  
65 sub _croak { goto &Regexp::Common::_croak }
66  
67 sub import {
68 my $self = shift;
69  
70 my $parent = ref tied %Regexp::Common::RE;
71 $parent ||= 'Regexp::Common';
72 push @ISA, $parent unless $self->isa($parent);
73  
74 tie %Regexp::Common::RE, __PACKAGE__
75 if !defined tied %Regexp::Common::RE
76 || !tied( %Regexp::Common::RE )->isa(__PACKAGE__);
77  
78 {
79 no strict 'refs';
80 *{caller() . "::RE"} = \%Regexp::Common::RE;
81 }
82 }
83  
84 sub action {
85 my ($self, $name, @rest) = @_;
86 $name = '' unless defined $name;
87 my $entry = $self->_decache;
88  
89 my $key = join '_', grep !/^-/, @{ $entry->{'args'} };
90  
91 my $action = $ACTION{'static'}{$key}{$name};
92 return $action if $action;
93  
94 my $generator = $ACTION{'generated'}{$key}{$name};
95 unless ( $generator ) {
96 if ( length $name ) {
97 _croak "Regexp has no action '$name'";
98 } else {
99 _croak 'Regexp has no default action';
100 }
101 }
102  
103 return $generator->(
104 $entry, $entry->{flags}, $entry->{args},
105 $name, @rest
106 );
107 }
108  
109 package Regexp::Common::WithActions::Actions;
110 sub _croak { goto &Regexp::Common::_croak }
111  
112 sub gen_quoter {
113 my ($dels, $escs) = @_;
114  
115 my $res;
116 if ( length $escs ) {
117 substr ($dels, 1) = '' foreach $dels, $escs;
118 $res = sub {
119 my $s = defined wantarray? \"$_[0]": \$_[0];
120 $$s =~ s/(\Q$dels\E|\Q$dels\E)/$escs$1/g;
121 substr($$s, 0, 0) = $dels;
122 $$s .= $dels;
123 $$s;
124 }
125 } else {
126 my @dels = split //, $dels;
127 $res = sub {
128 my $del;
129 foreach ( @dels ) {
130 next if index($_[0], $_) >= 0;
131 $del = $_; last;
132 }
133 _croak "Can not quote, string contains all possible delimiters"
134 unless defined $del;
135  
136 my $s = defined wantarray? \"$_[0]": \$_[0];
137 substr($$s, 0, 0) = $del;
138 $$s .= $del;
139 $$s;
140 }
141 }
142 return $res;
143 }
144  
145 sub gen_dequoter {
146 my ($dels, $escs) = @_;
147  
148 my $res;
149 if ( length $escs ) {
150 $escs .= substr ($escs, -1) x (length ($dels) - length ($escs));
151 my %del = map { $_ => (substr($escs,0,1,'')) } split //, $dels;
152 $res = sub {
153 my $esc = $del{ substr($_[0], 0, 1) };
154 return $_[0] unless defined $esc
155 && substr($_[0], 0, 1) eq substr($_[0], -1);
156  
157 my $s = defined wantarray? \"$_[0]": \$_[0];
158 my $del = substr($$s, 0, 1, '');
159 substr($$s, -1) = '';
160 if ( $del ne $esc ) {
161 $$s =~ s/\Q$esc\E(.)/$1/g;
162 } else {
163 $$s =~ s/\Q$del$del/$del/g;
164 }
165 $$s;
166 }
167 } else {
168 my %del = map {$_=>1} split //, $dels;
169 $res = sub {
170 return $_[0] unless $del{ substr($_[0], 0, 1) }
171 && substr($_[0], 0, 1) eq substr($_[0], -1);
172 my $s = defined wantarray? \"$_[0]": \$_[0];
173 substr($$s, 0, 1) = '';
174 substr($$s, -1) = '';
175 $$s
176 }
177 }
178 return $res;
179 }
180  
181  
182  
183 $Regexp::Common::WithActions::ACTION{'generated'}{'delimited'} = {
184 quote => sub {
185 return gen_quoter (@{$_[1]}{-delim, -esc});
186 },
187 dequote => sub {
188 return gen_dequoter (@{$_[1]}{-delim, -esc});
189 },
190 };
191  
192 $Regexp::Common::WithActions::ACTION{'generated'}{'quoted'} = {
193 quote => sub {
194 return gen_quoter (@{$_[1]}{-delim, -esc});
195 },
196 dequote => sub {
197 return gen_dequoter (@{$_[1]}{-delim, -esc});
198 },
199 };
200  
201 1;
202  
203 =head1 AUTHOR
204  
205 Ruslan.Zakirov@gmail.com
206  
207 =head1 LICENSE
208  
209 Under the same terms as perl itself.
210  
211 =cut
212