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