| 1 | package Regexp::Pattern; | ||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||
| 3 | 2 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||||||||||||||||||||||||||||
| 4 | 2 | our $DATE = '2020-04-01'; # DATE | |||||||||||||||||||||||||||||||||||||
| 5 | 2 | our $DIST = 'Regexp-Pattern'; # DIST | |||||||||||||||||||||||||||||||||||||
| 6 | 2 | our $VERSION = '0.2.14'; # VERSION | |||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use strict 'subs', 'vars'; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 9 | #use warnings; | ||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||
| 11 | 48 | ▶ | sub re { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
re: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 12 | 48 | my $name = shift; | |||||||||||||||||||||||||||||||||||||
| 13 | 48 | ▶ | my %args = ref($_[0]) eq 'HASH' ? %{$_[0]} : @_; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||
| 15 | 48 | ▶ | my ($mod, $patname) = $name =~ /(.+)::(.+)/ | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 16 | or die "Invalid pattern name '$name', should be 'MODNAME::PATNAME'"; | ||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||
| 18 | 48 | $mod = "Regexp::Pattern::$mod"; | |||||||||||||||||||||||||||||||||||||
| 19 | 48 | (my $mod_pm = "$mod.pm") =~ s!::!/!g; | |||||||||||||||||||||||||||||||||||||
| 20 | 48 | require $mod_pm; | |||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||
| 22 | 48 | my $var = \%{"$mod\::RE"}; | |||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||
| 24 | 48 | ▶ | exists($var->{$patname}) | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 25 | or die "No regexp pattern named '$patname' in package '$mod'"; | ||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||
| 27 | 47 | my $pat; | |||||||||||||||||||||||||||||||||||||
| 28 | 47 | ▶ | if ($var->{$patname}{pat}) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||
| 29 | 34 | $pat = $var->{$patname}{pat}; | |||||||||||||||||||||||||||||||||||||
| 30 | } elsif ($var->{$patname}{gen}) { | ||||||||||||||||||||||||||||||||||||||
| 31 | 13 | $pat = $var->{$patname}{gen}->(%args); | |||||||||||||||||||||||||||||||||||||
| 32 | } else { | ||||||||||||||||||||||||||||||||||||||
| 33 | 0 | die "Bug in module '$mod': pattern '$patname': no pat/gen declared"; | |||||||||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||
| 36 | 47 | ▶ | if ($args{-anchor}) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 37 | 3 | ▶ | if ($args{-anchor} eq 'left') { | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||
| 38 | 1 | $pat = qr/\A(?:$pat)/; | |||||||||||||||||||||||||||||||||||||
| 39 | } elsif ($args{-anchor} eq 'right') { | ||||||||||||||||||||||||||||||||||||||
| 40 | 1 | $pat = qr/(?:$pat)\z/; | |||||||||||||||||||||||||||||||||||||
| 41 | } else { | ||||||||||||||||||||||||||||||||||||||
| 42 | 1 | $pat = qr/\A(?:$pat)\z/; | |||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||
| 46 | 47 | return $pat; | |||||||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||
| 49 | 24 | ▶ | sub import { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
import: called CC=36 SCAR=35.8
| |||||||||||||||||||||||||||||||||||||||
| 50 | 24 | my $package = shift; | |||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||
| 52 | 24 | my $caller = caller(); | |||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||
| 54 | 24 | my @args = @_; | |||||||||||||||||||||||||||||||||||||
| 55 | 24 | ▶ | @args = ('re') unless @args; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||
| 57 | 24 | ▶ | while (@args) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 58 | 25 | my $arg = shift @args; | |||||||||||||||||||||||||||||||||||||
| 59 | 25 | my ($mod, $name0, $as, $prefix, $suffix, | |||||||||||||||||||||||||||||||||||||
| 60 | $has_tag, $lacks_tag, $has_tag_matching, $lacks_tag_matching, $gen_args); | ||||||||||||||||||||||||||||||||||||||
| 61 | 25 | ▶ | if ($arg eq 're') { | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||
| 62 | 1 | *{"$caller\::re"} = \&re; | |||||||||||||||||||||||||||||||||||||
| 63 | 1 | next; | |||||||||||||||||||||||||||||||||||||
| 64 | } elsif ($arg =~ /\A(\w+(?:::\w+)*)::(\w+|\*)\z/) { | ||||||||||||||||||||||||||||||||||||||
| 65 | 22 | ($mod, $name0) = ($1, $2); | |||||||||||||||||||||||||||||||||||||
| 66 | 22 | ($as, $prefix, $suffix, $has_tag, $lacks_tag, $has_tag_matching, $lacks_tag_matching) = | |||||||||||||||||||||||||||||||||||||
| 67 | (undef, undef, undef, undef, undef); | ||||||||||||||||||||||||||||||||||||||
| 68 | 22 | $gen_args = {}; | |||||||||||||||||||||||||||||||||||||
| 69 | 22 | ▶ | while (@args >= 2 && $args[0] =~ /\A-?\w+\z/) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@args >= 2 and $args[0] =~ /\A-?\w+\z/
MC/DC50%1/2
@args >= 2 and $args[0] =~ /\A-?\w+\z/
@args >= 2
$args[0] =~ /\A-?\w+\z/
Truth table66%2/3
A and B
A= @args >= 2B= $args[0] =~ /\A-?\w+\z/
| |||||||||||||||||||||||||||||||||||||||
| 70 | 20 | my ($k, $v) = splice @args, 0, 2; | |||||||||||||||||||||||||||||||||||||
| 71 | 20 | ▶ | if ($k eq '-as') { | ||||||||||||||||||||||||||||||||||||
|
Branch: 8 branches
| |||||||||||||||||||||||||||||||||||||||
| 72 | 3 | ▶ | die "Cannot use -as on a wildcard import '$arg'" | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 73 | if $name0 eq '*'; | ||||||||||||||||||||||||||||||||||||||
| 74 | 2 | ▶ | die "Please use a simple identifier for value of -as" | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 75 | unless $v =~ /\A\w+\z/; | ||||||||||||||||||||||||||||||||||||||
| 76 | 1 | $as = $v; | |||||||||||||||||||||||||||||||||||||
| 77 | } elsif ($k eq '-prefix') { | ||||||||||||||||||||||||||||||||||||||
| 78 | 3 | $prefix = $v; | |||||||||||||||||||||||||||||||||||||
| 79 | } elsif ($k eq '-suffix') { | ||||||||||||||||||||||||||||||||||||||
| 80 | 2 | $suffix = $v; | |||||||||||||||||||||||||||||||||||||
| 81 | } elsif ($k eq '-has_tag') { | ||||||||||||||||||||||||||||||||||||||
| 82 | 4 | $has_tag = $v; | |||||||||||||||||||||||||||||||||||||
| 83 | } elsif ($k eq '-lacks_tag') { | ||||||||||||||||||||||||||||||||||||||
| 84 | 2 | $lacks_tag = $v; | |||||||||||||||||||||||||||||||||||||
| 85 | } elsif ($k eq '-has_tag_matching') { | ||||||||||||||||||||||||||||||||||||||
| 86 | 2 | ▶ | $has_tag_matching = ref $v eq 'Regexp' ? $v : qr/$v/; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 87 | } elsif ($k eq '-lacks_tag_matching') { | ||||||||||||||||||||||||||||||||||||||
| 88 | 2 | ▶ | $lacks_tag_matching = ref $v eq 'Regexp' ? $v : qr/$v/; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 89 | } elsif ($k !~ /\A-/) { | ||||||||||||||||||||||||||||||||||||||
| 90 | 1 | $gen_args->{$k} = $v; | |||||||||||||||||||||||||||||||||||||
| 91 | } else { | ||||||||||||||||||||||||||||||||||||||
| 92 | 1 | die "Unknown import option '$k'"; | |||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||
| 95 | } else { | ||||||||||||||||||||||||||||||||||||||
| 96 | 2 | die "Invalid import '$arg', either specify 're' or a qualified ". | |||||||||||||||||||||||||||||||||||||
| 97 | "pattern name e.g. 'Foo::bar', which can be followed by ". | ||||||||||||||||||||||||||||||||||||||
| 98 | "name-value pairs"; | ||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||
| 101 | 19 | *{"$caller\::RE"} = \%{"$caller\::RE"}; | |||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||
| 103 | 19 | my @names; | |||||||||||||||||||||||||||||||||||||
| 104 | 19 | ▶ | if ($name0 eq '*') { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 105 | 12 | my $mod = "Regexp::Pattern::$mod"; | |||||||||||||||||||||||||||||||||||||
| 106 | 12 | (my $mod_pm = "$mod.pm") =~ s!::!/!g; | |||||||||||||||||||||||||||||||||||||
| 107 | 12 | require $mod_pm; | |||||||||||||||||||||||||||||||||||||
| 108 | 12 | my $var = \%{"$mod\::RE"}; | |||||||||||||||||||||||||||||||||||||
| 109 | 12 | for my $n (sort keys %$var) { | |||||||||||||||||||||||||||||||||||||
| 110 | 60 | ▶ | my $tags = $var->{$n}{tags} || []; | ||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$var->{$n}{'tags'} || []
MC/DC100%1/1
$var->{$n}{'tags'} || []
$var->{$n}{'tags'}
Truth table100%2/2
A
A= $var->{$n}{'tags'}
| |||||||||||||||||||||||||||||||||||||||
| 111 | 60 | ▶ | if (defined $has_tag) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 112 | 20 | ▶ | next unless grep { $_ eq $has_tag } @$tags; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||
| 114 | 45 | ▶ | if (defined $lacks_tag) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 115 | 7 | ▶ | next if grep { $_ eq $lacks_tag } @$tags; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||
| 117 | 43 | ▶ | if (defined $has_tag_matching) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 118 | 10 | ▶ | next unless grep { $_ =~ $has_tag_matching } @$tags; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||
| 120 | 37 | ▶ | if (defined $lacks_tag_matching) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 121 | 7 | ▶ | next if grep { $_ =~ $lacks_tag_matching } @$tags; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||
| 123 | 34 | push @names, $n; | |||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||
| 125 | 12 | ▶ | unless (@names) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 126 | 1 | warn "No patterns imported in wildcard import '$mod\::*'"; | |||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||
| 128 | } else { | ||||||||||||||||||||||||||||||||||||||
| 129 | 7 | @names = ($name0); | |||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||
| 131 | 19 | for my $n (@names) { | |||||||||||||||||||||||||||||||||||||
| 132 | 41 | ▶ | my $name = defined($as) ? $as : | ||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||
| 133 | (defined $prefix ? $prefix : "") . $n . | ||||||||||||||||||||||||||||||||||||||
| 134 | (defined $suffix ? $suffix : ""); | ||||||||||||||||||||||||||||||||||||||
| 135 | 41 | ▶ | if (exists ${"$caller\::RE"}{$name}) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 136 | 1 | warn "Overwriting pattern '$name' by importing '$mod\::$n'"; | |||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||
| 138 | 41 | ${"$caller\::RE"}{$name} = re("$mod\::$n", $gen_args); | |||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||
| 143 | 2 | 1; | |||||||||||||||||||||||||||||||||||||
| 144 | # ABSTRACT: Convention/framework for modules that contain collection of regexes | ||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||
| 146 | __END__ | ||||||||||||||||||||||||||||||||||||||