Hopen/Util/NameSet.pm

filter: Statementstmt 100% 48 / 48 Branchbran 100% 26 / 26 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 12 / 12 Podpod 100% 6 / 6 totaltotal 100% 92 / 92 CC 15 SCAR 14.1
CC
15
Cov
100%
add
8.0
_build
4.0
new
3.0
CRAP 4.1
« Hopen/Scope.pm ↑ Summary Hopen.pm »
1 # Data::Hopen::Util::NameSet - set of strings and regexps
2 package Data::Hopen::Util::NameSet;
3 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 23 ▶use Data::Hopen::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 23 our $VERSION = '0.000018';
7  
8 # Docs {{{1
9  
10 =head1 NAME
11  
12 Data::Hopen::Util::NameSet - set of names (strings or regexps)
13  
14 =head1 SYNOPSIS
15  
16 NameSet stores strings and regexps, and can quickly tell you whether
17 a given string matches one of the stored strings or regexps.
18  
19 =cut
20  
21 # }}}1
22  
23 =head1 FUNCTIONS
24  
25 =head2 new
26  
27 Create a new instance. Usage: C<< Data::Hopen::Util::Nameset->new(...) >>.
28 The parameters are as L</add>.
29  
30 =cut
31  
32 sub new {
33 53 ▶ my $class = shift or croak 'Call as ' . __PACKAGE__ . '->new(...)';
Subroutine new: called CC=3 SCAR=11.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
my $class = shift() or croak("Call as Data::Hopen::Util::NameSet->new(...)") 1 52 53
34 52 my $self = bless { _strings => [], _regexps => [], _RE => undef }, $class;
35 52 ▶ $self->add(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 27 25 52
36 51 return $self;
37 } #new()
38  
39 =head2 add
40  
41 Add one or more strings or regexps to the NameSet. Usage:
42  
43 $instance->add(x1, x2, ...)
44  
45 where each C<xn> can be a scalar, regexp, arrayref (processed recursively)
46 or hashref (the keys are added and the values are ignored).
47  
48 =cut
49  
50 sub add {
51 40 ▶ my $self = shift or croak 'Need an instance';
Subroutine add: called CC=8 SCAR=20.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak("Need an instance") 1 39 40
52 39 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 38 39
53 38 $self->{_RE} = undef; # dirty the instance
54  
55 38 foreach my $arg (@_) {
56 61 ▶ if(!ref $arg) {
Branch: 4 branches
BranchTrueFalseTotal
if (not ref $arg) { } 24 37 61
elsif (ref $arg eq 'Regexp') { } 29 8 37
elsif (ref $arg eq 'ARRAY') { } 4 4 8
elsif (ref $arg eq 'HASH') { } 3 1 4
57 24 push @{$self->{_strings}}, "$arg";
58 } elsif(ref $arg eq 'Regexp') {
59 29 push @{$self->{_regexps}}, $arg;
60 } elsif(ref $arg eq 'ARRAY') {
61 4 $self->add(@$arg);
62 } elsif(ref $arg eq 'HASH') {
63 3 $self->add(keys %$arg);
64 } else {
65 23 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 1 croak "I don't know how to handle this: " . Dumper($arg)
67 }
68 }
69 } #add()
70  
71 =head2 contains
72  
73 Return truthy if the NameSet contains the argument. Usage:
74 C<< $set->contains('foo') >>.
75  
76 =cut
77  
78 sub contains {
79 95 ▶ my $self = shift or croak 'Need an instance';
Subroutine contains: called CC=3 SCAR=11.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak("Need an instance") 1 94 95
80 94 ▶ $self->{_RE} = $self->_build unless $self->{_RE}; # Clean
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_RE"} 7 87 94
81 #say STDERR $self->{_RE};
82 94 return shift =~ $self->{_RE};
83 } #contains()
84  
85 =head2 smartmatch overload
86  
87 For convenience, C<< 'foo' ~~ $nameset >> invokes
88 C<< $nameset->contains('foo') >>. This is inspired by the Raku behaviour,
89 in which C<< $x ~~ $y >> calls C<< $y.ACCEPTS($x) >>
90  
91 NOTE: C<< $nameset ~~ 'foo' >> (object first) is officially not supported by
92 this module. This form is possible in stable perls at least through 5.26.
93 However, the changes (since reverted) in 5.27.7 would not have supported this
94 form. See
95 L<http://blogs.perl.org/users/leon_timmermans/2017/12/smartmatch-in-5277.html>.
96 However, as far as I can tell, even 5.27.7 would have supported the
97 C<< 'foo' ~~ $nameset >> form.
98  
99 =cut
100  
101 use overload
102 fallback => 1,
103 '~~' => sub {
104 #my ($self, $other, $swap) = @_;
105 69 ▶ $_[0]->contains($_[1])
Subroutine __ANON__: called CC=1 SCAR=0.0
106 23 ▶ };
Subroutine BEGIN: called CC=1 SCAR=0.0
107  
108 =head2 strings
109  
110 Accessor for the strings in the set. Returns an arrayref.
111  
112 =cut
113  
114 2 ▶sub strings { (shift)->{_strings} }
Subroutine strings: called CC=1 SCAR=0.0
Pod documented
115  
116 =head2 regexps
117  
118 Accessor for the regexps in the set. Returns an arrayref.
119  
120 =cut
121  
122 2 ▶sub regexps { (shift)->{_regexps} }
Subroutine regexps: called CC=1 SCAR=0.0
Pod documented
123  
124 =head2 complex
125  
126 Returns truthy if the nameset has any regular expressions.
127  
128 =cut
129  
130 4 ▶sub complex { @{(shift)->{_regexps}} > 0 }
Subroutine complex: called CC=1 SCAR=0.0
Pod documented
131  
132 =head2 _build
133  
134 (Internal) Build a regex from all the strings and regexps in the set.
135 Returns the new regexp --- does not mutate $self.
136  
137 In the current implementation, strings are matched case-sensitively.
138 Regexps are matched with whatever flags they were compiled with.
139  
140 =cut
141  
142 sub _build {
143 8 ▶ my $self = shift or croak 'Need an instance';
Subroutine _build: called CC=4 SCAR=13.9
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak("Need an instance") 1 7 8
144  
145 7 my @quoted_strs;
146 7 ▶ if(@{$self->{_strings}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"_strings"};}) 3 4 7
147 push @quoted_strs,
148 3 join '|', map { quotemeta } @{$self->{_strings}};
149 # TODO should I be using qr/\Q$_\E/ instead, since quotemeta
150 # isn't quite right on 5.14? Or should I be using 5.16+?
151 }
152  
153 7 my $pattern = join '|', @{$self->{_regexps}}, @quoted_strs;
154 # Each regexp stringifies with surrounding parens, so we
155 # don't need to add any.
156  
157 7 ▶ return $pattern ? qr/\A(?:$pattern)\z/ : qr/(*FAIL)/;
Branch: 1 branch
BranchTrueFalseTotal
$pattern ? : 5 2 7
158 # If $pattern is empty, the nameset is empty (`(*FAIL)`). Without the
159 # ?:, qr// would match anything, when we want to match nothing.
160 } #_build()
161  
162 23 1;
163 __END__
« Hopen/Scope.pm ↑ Summary Hopen.pm »