| 1 | package Regexp::Flow; | ||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | ▶ | use Exporter 'import'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 1 | our @EXPORT_OK = qw(re_matches re_substitutions); | |||||||||||||||||||
| 6 | 1 | ▶ | use Regexp::Flow::Result; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 1 | ▶ | use Regexp::Flow::Results; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | =head1 NAME | ||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | Regexp::Flow - flow control for using regular expression | ||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | =cut | ||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 1 | our $VERSION = '0.002'; | |||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | =head1 SYNOPSIS | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | use Regexp::Flow qw(re_matches re_substitutions); | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | ... | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | my $m_results = | ||||||||||||||||||||
| 24 | re_matches ( $string, $re, $code, $flags ); | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | my $s_results = | ||||||||||||||||||||
| 27 | re_substitutions ( $string, $re, $code, $flags ); | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | ... | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | foreach (@$m_results) { | ||||||||||||||||||||
| 32 | print $_->match; # assuming you used the /p flag | ||||||||||||||||||||
| 33 | } | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | re_matches ( ... ) or warn 'no matches'; | ||||||||||||||||||||
| 36 | # which is easier than using while, which doesn't allow or/else | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | =head1 FUNCTIONS | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | =cut | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | =head3 re_matches | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | my $results = re_matches ( $string, $re, $code, $flags ); | ||||||||||||||||||||
| 45 | my $results = re_matches ( $string, $re, $flags ); | ||||||||||||||||||||
| 46 | say $_->prematch for re_matches('1.23', qr/\D/p,''); #? | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | Finds all instances of C<$re> within C<string> and runs C<$code> each | ||||||||||||||||||||
| 49 | time a match is found. A M<Regexp:Flow::Result> object will be | ||||||||||||||||||||
| 50 | created and passed as the first argument to C<$code>. | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | If C<$flags> is not present, C<g> will be assumed. If not, you must | ||||||||||||||||||||
| 53 | include it yourself. | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | If the third argument is a string, it will be used as the flags. | ||||||||||||||||||||
| 56 | Otherwise, it will be executed as a coderef on the | ||||||||||||||||||||
| 57 | M<Regexp::Flow::Result> object, i.e. C<< $code->($rfr) >> | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | Within C<$code>, you can call C<last> on C<$rfr> to stop executing | ||||||||||||||||||||
| 60 | C<$code> any more. | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | Note: Remember you can use any of C<msixpodual> on the regexp and do | ||||||||||||||||||||
| 63 | not need to put these in C<$flags>. | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | So, for instance, to print C<$1> the first time it contains a word | ||||||||||||||||||||
| 66 | character you could do: | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | my $code = sub { | ||||||||||||||||||||
| 69 | my $rr = shift; | ||||||||||||||||||||
| 70 | if ($rr->c(1) =~ /\w/) { | ||||||||||||||||||||
| 71 | print $rr->c(1); | ||||||||||||||||||||
| 72 | $rr->last; | ||||||||||||||||||||
| 73 | } | ||||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | my $string = q{'', 'a', 'b'}); | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | re_matches ($string, qr/'([^']+)',?/, $code); | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | The return value of C<$code> is discarded (this may change). | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | In scalar context, the return value is a L<Regexp::Flow::Results> | ||||||||||||||||||||
| 83 | object (which evaluates to the number of times a match was found, and | ||||||||||||||||||||
| 84 | allows access to each of the results contained within). | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | In void context, this value is not returned. | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | In list context, should it return each result? | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | =cut | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 5 | ▶ | sub re_matches { | ||||||||||||||||||
|
Subroutine
re_matches: called CC=9 SCAR=22.1
Pod
documented
| |||||||||||||||||||||
| 93 | 5 | my $string = shift; | |||||||||||||||||||
| 94 | 5 | my $re = shift; | |||||||||||||||||||
| 95 | 5 | my $code = shift; | |||||||||||||||||||
| 96 | 5 | my $flags = 'g'; | |||||||||||||||||||
| 97 | 5 | ▶ | if (!ref $code) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 98 | 2 | ▶ | $flags = $code if defined $code; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 99 | 2 | ▶ | $code = sub {}; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 100 | } | ||||||||||||||||||||
| 101 | elsif (@_) { | ||||||||||||||||||||
| 102 | 1 | ▶ | $flags = shift // $flags; | ||||||||||||||||||
|
Condition33%1/3
shift() // $flags
MC/DC0%0/2
shift() // $flags
shift()
$flags
Truth table33%1/3
A // B
A= shift()B= $flags
| |||||||||||||||||||||
| 103 | } | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | 5 | my $results; | |||||||||||||||||||
| 106 | 5 | ▶ | if (defined wantarray) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 107 | 3 | $results = Regexp::Flow::Results->new; | |||||||||||||||||||
| 108 | } | ||||||||||||||||||||
| 109 | 9 | ▶ | my $action = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 110 | 9 | my $rfr = shift; | |||||||||||||||||||
| 111 | 9 | $rfr->string($string); | |||||||||||||||||||
| 112 | 9 | $rfr->re($re); | |||||||||||||||||||
| 113 | 9 | ▶ | if (defined $results) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 114 | 4 | push @{$results->contents}, $rfr; | |||||||||||||||||||
| 115 | } | ||||||||||||||||||||
| 116 | 9 | my $returnvalue = $code->($rfr); | |||||||||||||||||||
| 117 | 9 | $returnvalue; | |||||||||||||||||||
| 118 | 5 | }; | |||||||||||||||||||
| 119 | 5 | ▶ | die unless $flags =~ /^[a-z]+$/; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 120 | 5 | ▶ | if ($flags =~ m/g/) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 121 | 4 | eval qq` | |||||||||||||||||||
| 122 | while (\$string =~ m/\$re/$flags) { | ||||||||||||||||||||
| 123 | my \$rfr = Regexp::Flow::Result->new; | ||||||||||||||||||||
| 124 | \$action->(\$rfr); | ||||||||||||||||||||
| 125 | last if 'last' eq \$rfr->continue_action; | ||||||||||||||||||||
| 126 | } | ||||||||||||||||||||
| 127 | `; #~ we use the string eval to put flags in there. | ||||||||||||||||||||
| 128 | } | ||||||||||||||||||||
| 129 | else { | ||||||||||||||||||||
| 130 | 1 | eval qq` | |||||||||||||||||||
| 131 | \$action->( | ||||||||||||||||||||
| 132 | Regexp::Flow::Result->new | ||||||||||||||||||||
| 133 | ) if \$string =~ m/\$re/$flags; | ||||||||||||||||||||
| 134 | `; | ||||||||||||||||||||
| 135 | } | ||||||||||||||||||||
| 136 | 5 | ▶ | if ($@) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 137 | 0 | die ($@); | |||||||||||||||||||
| 138 | } | ||||||||||||||||||||
| 139 | 5 | return $results; | |||||||||||||||||||
| 140 | } | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | =head3 re_substitutions | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | my $results = re_substitutions ( $string, $re, $code, $flags ); | ||||||||||||||||||||
| 145 | my $results = re_substitutions ( $string, $re, $code ); | ||||||||||||||||||||
| 146 | my $results = re_substitutions ( $string, $re, $string ); | ||||||||||||||||||||
| 147 | my $results = re_substitutions ( $string, $re ); | ||||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | Finds all instances of C<$re> within C<$string> and runs C<$code> each | ||||||||||||||||||||
| 150 | time a match is found. A L<Regexp:Flow::Result> object will be | ||||||||||||||||||||
| 151 | created and passed as the first argument to C<$code>. | ||||||||||||||||||||
| 152 | The return value of C<$code> is used as the replacement for the | ||||||||||||||||||||
| 153 | matched string. If a string is passed as the third argument, it | ||||||||||||||||||||
| 154 | (C<$string>) will be the replcement. Therefore B<do not> pass flags | ||||||||||||||||||||
| 155 | as the third argument. | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | Just like C<s///>, this makes changes to the source string, unless | ||||||||||||||||||||
| 158 | the C<r> flag is present, in which case the source string will be | ||||||||||||||||||||
| 159 | untouched and the return value will be the modified string. | ||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | If flags are not provided, C<g> is assumed. | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | =cut | ||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | 1 | ▶ | sub re_substitutions { | ||||||||||||||||||
|
Subroutine
re_substitutions: called CC=9 SCAR=23.8
Pod
documented
| |||||||||||||||||||||
| 166 | 1 | my ($string, $re, $code, $flags) = @_; #~ we need to leave them in @_ to do in-place substitution | |||||||||||||||||||
| 167 | 1 | ▶ | if (!ref $code) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 168 | 0 | ▶ | $code = sub {$code}; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 169 | } | ||||||||||||||||||||
| 170 | 1 | ▶ | $flags //= 'g'; | ||||||||||||||||||
|
Condition50%1/2
$flags //= "g"
MC/DC0%0/1
$flags //= "g"
$flags
Truth table50%1/2
A
A= $flags
| |||||||||||||||||||||
| 171 | 1 | ▶ | my $rflag = ($flags =~ /r/ ? 1 :0 ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 172 | 1 | my $results; | |||||||||||||||||||
| 173 | 1 | ▶ | if (defined wantarray) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 174 | 1 | $results = Regexp::Flow::Results->new; | |||||||||||||||||||
| 175 | } | ||||||||||||||||||||
| 176 | 1 | my $last = 0; | |||||||||||||||||||
| 177 | 3 | ▶ | my $action = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 178 | 3 | my $rfr = shift; | |||||||||||||||||||
| 179 | 3 | ▶ | if (defined $results) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 180 | 3 | push @{$results->contents}, $rfr; | |||||||||||||||||||
| 181 | } | ||||||||||||||||||||
| 182 | 3 | my $returnvalue = $code->($rfr); | |||||||||||||||||||
| 183 | 3 | ▶ | $last = 1 if 'last' eq $rfr->continue_action; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 184 | 3 | $returnvalue; | |||||||||||||||||||
| 185 | 1 | }; | |||||||||||||||||||
| 186 | 1 | ▶ | die ('Unexpected flags [a-z] only permitted in '.$flags) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 187 | unless $flags =~ /^[a-z]+$/; | ||||||||||||||||||||
| 188 | #~ In the following code, We will be using s~~~e | ||||||||||||||||||||
| 189 | 1 | eval qq` | |||||||||||||||||||
| 190 | \$string =~ s~\$re~ | ||||||||||||||||||||
| 191 | my \$rfr = Regexp::Flow::Result->new; | ||||||||||||||||||||
| 192 | \$rfr->string(\$string); | ||||||||||||||||||||
| 193 | \$rfr->re(\$re); | ||||||||||||||||||||
| 194 | if (!\$last) { | ||||||||||||||||||||
| 195 | \$action->(\$rfr); | ||||||||||||||||||||
| 196 | } | ||||||||||||||||||||
| 197 | else { | ||||||||||||||||||||
| 198 | \$rfr->match; | ||||||||||||||||||||
| 199 | } | ||||||||||||||||||||
| 200 | ~e$flags | ||||||||||||||||||||
| 201 | `; #~ we use the string eval to put flags in there. | ||||||||||||||||||||
| 202 | 1 | ▶ | if ($@) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 203 | 0 | warn ($@); | |||||||||||||||||||
| 204 | } | ||||||||||||||||||||
| 205 | 1 | ▶ | if ($rflag) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 206 | 0 | return $string; | |||||||||||||||||||
| 207 | } | ||||||||||||||||||||
| 208 | #~ implicit else | ||||||||||||||||||||
| 209 | 1 | ▶ | $_[0] = $string if $results; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 210 | 1 | return $results; | |||||||||||||||||||
| 211 | } | ||||||||||||||||||||
| 212 | |||||||||||||||||||||
| 213 | =head1 SEE ALSO | ||||||||||||||||||||
| 214 | |||||||||||||||||||||
| 215 | Regexp::Result - base class for information about a regexp match | ||||||||||||||||||||
| 216 | |||||||||||||||||||||
| 217 | Regexp::Flow::Result - the class available within coderefs above | ||||||||||||||||||||
| 218 | |||||||||||||||||||||
| 219 | Regexp::Flow::Results - the list of results returned by functions above | ||||||||||||||||||||
| 220 | |||||||||||||||||||||
| 221 | =head1 BUGS | ||||||||||||||||||||
| 222 | |||||||||||||||||||||
| 223 | Please report any bugs or feature requests to the github issues tracker | ||||||||||||||||||||
| 224 | at L<https://github.com/pdl/Regexp-Result/issues>. I will be notified, | ||||||||||||||||||||
| 225 | and then you'll automatically be notified of progress on your bug as I | ||||||||||||||||||||
| 226 | make changes. | ||||||||||||||||||||
| 227 | |||||||||||||||||||||
| 228 | =head1 AUTHORS | ||||||||||||||||||||
| 229 | |||||||||||||||||||||
| 230 | Daniel Perrett | ||||||||||||||||||||
| 231 | |||||||||||||||||||||
| 232 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||
| 233 | |||||||||||||||||||||
| 234 | Copyright 2012-2013 Daniel Perrett. | ||||||||||||||||||||
| 235 | |||||||||||||||||||||
| 236 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||
| 237 | under the terms of either: the GNU General Public License as published | ||||||||||||||||||||
| 238 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||||||||
| 239 | |||||||||||||||||||||
| 240 | See L<http://dev.perl.org/licenses/> for more information. | ||||||||||||||||||||
| 241 | |||||||||||||||||||||
| 242 | =cut | ||||||||||||||||||||
| 243 | |||||||||||||||||||||
| 244 | 1 | 1; | |||||||||||||||||||
| 245 | |||||||||||||||||||||
| 246 | |||||||||||||||||||||
| 247 | |||||||||||||||||||||