File Coverage

blib/lib/MooseX/Types/Perl.pm
Criterion Covered Total %
statement 12 12 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 16 16 100.0


line stmt bran cond sub pod time code
1             package MooseX::Types::Perl 0.101344;
2             # ABSTRACT: Moose types that check against Perl syntax
3              
4 1         8 use MooseX::Types -declare => [ qw(
5             DistName
6              
7             ModuleName
8             PackageName
9              
10             Identifier
11             SafeIdentifier
12              
13             LaxVersionStr
14             StrictVersionStr
15             VersionObject
16 1     1   28931 ) ];
  1         444003  
17              
18             #pod =head1 SYNOPSIS
19             #pod
20             #pod use MooseX::Types::Perl qw(
21             #pod DistName
22             #pod
23             #pod ModuleName
24             #pod PackageName
25             #pod
26             #pod Identifier
27             #pod SafeIdentifier
28             #pod
29             #pod LaxVersionStr
30             #pod StrictVersionStr
31             #pod VersionObject
32             #pod );
33             #pod
34             #pod =head1 DESCRIPTION
35             #pod
36             #pod This library provides L<Moose types|MooseX::Types> for checking things (mostly
37             #pod strings) against syntax that is, or is a reasonable subset of, Perl syntax.
38             #pod
39             #pod =cut
40              
41 1     1   7754 use MooseX::Types::Moose qw(Object Str);
  1         15340  
  1         9  
42 1     1   5081 use Params::Util qw(_CLASS);
  1         2  
  1         58  
43 1     1   518 use version 0.82;
  1         1592  
  1         7  
44              
45             #pod =head1 TYPES
46             #pod
47             #pod =head2 ModuleName
48             #pod
49             #pod =head2 PackageName
50             #pod
51             #pod These types are identical, and expect a string that could be a package or
52             #pod module name. That's basically a bunch of identifiers stuck together with
53             #pod double-colons. One key quirk is that parts of the package name after the
54             #pod first may begin with digits.
55             #pod
56             #pod The use of an apostrophe as a package separator is not permitted.
57             #pod
58             #pod =cut
59              
60             subtype ModuleName, as Str, where { ! /\P{ASCII}/ && _CLASS($_) };
61             subtype PackageName, as Str, where { ! /\P{ASCII}/ && _CLASS($_) };
62              
63             #pod =head2 DistName
64             #pod
65             #pod The DistName type checks for a string like C<MooseX-Types-Perl>, the sort of
66             #pod thing used to name CPAN distributions. In general, it's like the more familiar
67             #pod L<ModuleName>, but with hyphens instead of double-colons.
68             #pod
69             #pod In reality, a few distribution names may not match this pattern -- most
70             #pod famously, C<CGI.pm> is the name of the distribution that contains CGI. These
71             #pod exceptions are few and far between, and deciding what a C<LaxDistName> type
72             #pod would look like has not seemed worth it, yet.
73             #pod
74             #pod =cut
75              
76             subtype DistName,
77             as Str,
78             where {
79             return if /:/;
80             (my $str = $_) =~ s/-/::/g;
81             $str !~ /\P{ASCII}/ && _CLASS($str)
82             },
83             message {
84             /::/
85             ? "$_ looks like a module name, not a dist name"
86             : "$_ is not a valid dist name"
87             };
88              
89             # LaxDistName -- how does this work, other than "like some characters, okay?"
90              
91             #pod =head2 Identifier
92             #pod
93             #pod An L<Identifier|perldata/Variable names> is something that could be used as a
94             #pod symbol name or other identifier (filehandle, directory handle, subroutine name,
95             #pod format name, or label). It's what you put after the sigil (dollar sign, at
96             #pod sign, percent sign) in a variable name. Generally, it's a bunch of
97             #pod alphanumeric characters not starting with a digit.
98             #pod
99             #pod Although Perl identifiers may contain non-ASCII characters in some
100             #pod circumstances, this type does not allow it. A C<UnicodeIdentifier> type may be
101             #pod added in the future.
102             #pod
103             #pod =cut
104              
105             subtype Identifier,
106             as Str,
107             where { / \A [_a-z] [_a-z0-9]* \z /xi; };
108              
109             #pod =head2 SafeIdentifier
110             #pod
111             #pod SafeIdentifiers are just like Identifiers, but omit the single-letter variables
112             #pod underscore, a, and b, as these have special significance.
113             #pod
114             #pod =cut
115              
116             subtype SafeIdentifier,
117             as Identifier,
118             where { ! / \A [_ab] \z /x; };
119              
120             #pod =head2 LaxVersionStr
121             #pod
122             #pod =head2 StrictVersionStr
123             #pod
124             #pod Lax and strict version strings use the L<is_lax|version/is_lax> and
125             #pod L<is_strict|version/is_strict> methods from C<version> to check if the given
126             #pod string would be a valid lax or strict version. L<version::Internals> covers
127             #pod the details but basically: lax versions are everything you may do, and strict
128             #pod omit many of the usages best avoided.
129             #pod
130             #pod =cut
131              
132             subtype LaxVersionStr,
133             as Str,
134             where { version::is_lax($_) },
135             message { "$_ is not a valid lax version string" };
136              
137             subtype StrictVersionStr,
138             as LaxVersionStr,
139             where { version::is_strict($_) },
140             message { "$_ is not a valid strict version string" };
141              
142             #pod =head2 VersionObject
143             #pod
144             #pod Just for good measure, this type is included to check if a value is a version
145             #pod object. Coercions from LaxVersionStr (and thus StrictVersionStr) are provided.
146             #pod
147             #pod =cut
148              
149             subtype VersionObject,
150             as Object,
151             where { $_->isa('version') };
152              
153             coerce VersionObject,
154             from LaxVersionStr,
155             via { version->parse($_) };
156              
157             1;
158              
159             __END__
160              
161             =pod
162              
163             =encoding UTF-8
164              
165             =head1 NAME
166              
167             MooseX::Types::Perl - Moose types that check against Perl syntax
168              
169             =head1 VERSION
170              
171             version 0.101344
172              
173             =head1 SYNOPSIS
174              
175             use MooseX::Types::Perl qw(
176             DistName
177              
178             ModuleName
179             PackageName
180              
181             Identifier
182             SafeIdentifier
183              
184             LaxVersionStr
185             StrictVersionStr
186             VersionObject
187             );
188              
189             =head1 DESCRIPTION
190              
191             This library provides L<Moose types|MooseX::Types> for checking things (mostly
192             strings) against syntax that is, or is a reasonable subset of, Perl syntax.
193              
194             =head1 PERL VERSION
195              
196             This library should run on perls released even a long time ago. It should work
197             on any version of perl released in the last five years.
198              
199             Although it may work on older versions of perl, no guarantee is made that the
200             minimum required version will not be increased. The version may be increased
201             for any reason, and there is no promise that patches will be accepted to lower
202             the minimum required perl.
203              
204             =head1 TYPES
205              
206             =head2 ModuleName
207              
208             =head2 PackageName
209              
210             These types are identical, and expect a string that could be a package or
211             module name. That's basically a bunch of identifiers stuck together with
212             double-colons. One key quirk is that parts of the package name after the
213             first may begin with digits.
214              
215             The use of an apostrophe as a package separator is not permitted.
216              
217             =head2 DistName
218              
219             The DistName type checks for a string like C<MooseX-Types-Perl>, the sort of
220             thing used to name CPAN distributions. In general, it's like the more familiar
221             L<ModuleName>, but with hyphens instead of double-colons.
222              
223             In reality, a few distribution names may not match this pattern -- most
224             famously, C<CGI.pm> is the name of the distribution that contains CGI. These
225             exceptions are few and far between, and deciding what a C<LaxDistName> type
226             would look like has not seemed worth it, yet.
227              
228             =head2 Identifier
229              
230             An L<Identifier|perldata/Variable names> is something that could be used as a
231             symbol name or other identifier (filehandle, directory handle, subroutine name,
232             format name, or label). It's what you put after the sigil (dollar sign, at
233             sign, percent sign) in a variable name. Generally, it's a bunch of
234             alphanumeric characters not starting with a digit.
235              
236             Although Perl identifiers may contain non-ASCII characters in some
237             circumstances, this type does not allow it. A C<UnicodeIdentifier> type may be
238             added in the future.
239              
240             =head2 SafeIdentifier
241              
242             SafeIdentifiers are just like Identifiers, but omit the single-letter variables
243             underscore, a, and b, as these have special significance.
244              
245             =head2 LaxVersionStr
246              
247             =head2 StrictVersionStr
248              
249             Lax and strict version strings use the L<is_lax|version/is_lax> and
250             L<is_strict|version/is_strict> methods from C<version> to check if the given
251             string would be a valid lax or strict version. L<version::Internals> covers
252             the details but basically: lax versions are everything you may do, and strict
253             omit many of the usages best avoided.
254              
255             =head2 VersionObject
256              
257             Just for good measure, this type is included to check if a value is a version
258             object. Coercions from LaxVersionStr (and thus StrictVersionStr) are provided.
259              
260             =head1 AUTHOR
261              
262             Ricardo SIGNES <cpan@semiotic.systems>
263              
264             =head1 CONTRIBUTORS
265              
266             =for stopwords Karen Etheridge Ricardo Signes
267              
268             =over 4
269              
270             =item *
271              
272             Karen Etheridge <ether@cpan.org>
273              
274             =item *
275              
276             Ricardo Signes <rjbs@semiotic.systems>
277              
278             =back
279              
280             =head1 COPYRIGHT AND LICENSE
281              
282             This software is copyright (c) 2022 by Ricardo SIGNES.
283              
284             This is free software; you can redistribute it and/or modify it under
285             the same terms as the Perl 5 programming language system itself.
286              
287             =cut