| 1 | package MooseX::Types::Perl 0.101344; | ||||||||||
| 2 | # ABSTRACT: Moose types that check against Perl syntax | ||||||||||
| 3 | |||||||||||
| 4 | 1 | ▶ | use MooseX::Types -declare => [ qw( | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | DistName | ||||||||||
| 6 | |||||||||||
| 7 | ModuleName | ||||||||||
| 8 | PackageName | ||||||||||
| 9 | |||||||||||
| 10 | Identifier | ||||||||||
| 11 | SafeIdentifier | ||||||||||
| 12 | |||||||||||
| 13 | LaxVersionStr | ||||||||||
| 14 | StrictVersionStr | ||||||||||
| 15 | VersionObject | ||||||||||
| 16 | 1 | ) ]; | |||||||||
| 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 | ▶ | use MooseX::Types::Moose qw(Object Str); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 42 | 1 | ▶ | use Params::Util qw(_CLASS); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 43 | 1 | ▶ | use version 0.82; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 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 | 1 | ▶ | subtype ModuleName, as Str, where { ! /\P{ASCII}/ && _CLASS($_) }; | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
| |||||||||||
| 61 | 3 | ▶ | subtype PackageName, as Str, where { ! /\P{ASCII}/ && _CLASS($_) }; | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||
| 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 | 3 | ▶ | where { | ||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||
| 79 | 3 | ▶ | return if /:/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | 2 | (my $str = $_) =~ s/-/::/g; | |||||||||
| 81 | 2 | ▶ | $str !~ /\P{ASCII}/ && _CLASS($str) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | }, | ||||||||||
| 83 | 0 | ▶ | message { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 84 | 0 | ▶ | /::/ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | ? "$_ looks like a module name, not a dist name" | ||||||||||
| 86 | : "$_ is not a valid dist name" | ||||||||||
| 87 | 1 | }; | |||||||||
| 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 | 2 | ▶ | where { / \A [_a-z] [_a-z0-9]* \z /xi; }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 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 | 1 | ▶ | where { ! / \A [_ab] \z /x; }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 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 | 2 | ▶ | where { version::is_lax($_) }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 135 | 0 | ▶ | message { "$_ is not a valid lax version string" }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||
| 136 | |||||||||||
| 137 | subtype StrictVersionStr, | ||||||||||
| 138 | as LaxVersionStr, | ||||||||||
| 139 | 0 | ▶ | where { version::is_strict($_) }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 140 | 0 | ▶ | message { "$_ is not a valid strict version string" }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||
| 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 | 1 | ▶ | where { $_->isa('version') }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 152 | |||||||||||
| 153 | coerce VersionObject, | ||||||||||
| 154 | from LaxVersionStr, | ||||||||||
| 155 | 1 | ▶ | via { version->parse($_) }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 156 | |||||||||||
| 157 | 1 | 1; | |||||||||
| 158 | |||||||||||
| 159 | __END__ | ||||||||||