/usr/share/perl5/Specio/Subs.pm is in libspecio-perl 0.42-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | package Specio::Subs;
use strict;
use warnings;
our $VERSION = '0.42';
use Carp qw( croak );
use Eval::Closure qw( eval_closure );
use Module::Runtime qw( use_package_optimistically );
use Specio::Library::Perl;
use Specio::Registry qw( exportable_types_for_package );
my $counter = 0;
sub import {
shift;
my @libs = @_;
my $caller = caller();
my $ident = t('Identifier');
use_package_optimistically($_) for @libs;
for my $types ( map { exportable_types_for_package($_) } @libs ) {
for my $name ( keys %{$types} ) {
croak
qq{Cannot use '$name' type to create a check sub. It results in an invalid Perl subroutine name}
unless $ident->check( 'is_' . $name );
_export_subs( $name, $types->{$name}, $caller );
}
}
}
sub _export_subs {
my $name = shift;
my $type = shift;
my $caller = shift;
_export_validation_subs( $name, $type, $caller );
return unless $type->has_coercions;
_export_coercion_subs( $name, $type, $caller );
}
sub _export_validation_subs {
my $name = shift;
my $type = shift;
my $caller = shift;
my $is_name = 'is_' . $name;
my $assert_name = 'assert_' . $name;
if ( $type->can_be_inlined ) {
_make_sub(
$caller, $is_name,
$type->inline_check('$_[0]')
);
_make_sub(
$caller, $assert_name,
$type->inline_assert('$_[0]')
);
}
else {
_install_sub(
$caller, $is_name,
sub { $type->value_is_valid( $_[0] ) }
);
_install_sub(
$caller, $assert_name,
sub { $type->validate_or_die( $_[0] ) }
);
}
}
sub _export_coercion_subs {
my $name = shift;
my $type = shift;
my $caller = shift;
my $to_name = 'to_' . $name;
if ( $type->can_inline_coercion ) {
_make_sub(
$caller, $to_name,
$type->inline_coercion('$_[0]')
);
}
else {
_install_sub(
$caller, $to_name,
sub { $type->coerce_value( $_[0] ) }
);
}
my $force_name = 'force_' . $name;
if ( $type->can_inline_coercion_and_check ) {
_make_sub(
$caller, $force_name,
$type->inline_coercion_and_check('$_[0]')
);
}
else {
_install_sub(
$caller, $force_name,
sub {
my $val = $type->coerce_value( $_[0] );
$type->validate_or_die($val);
return $val;
}
);
}
}
sub _make_sub {
my $caller = shift;
my $sub_name = shift;
my $source = shift;
my $env = shift;
my $sub = eval_closure(
source => 'sub { ' . $source . ' }',
environment => $env,
description => $caller . '::'
. $sub_name
. ' generated by '
. __PACKAGE__,
);
_install_sub( $caller, $sub_name, $sub );
return;
}
my $sub_namer = do {
eval {
require Sub::Util;
Sub::Util->VERSION(1.40);
Sub::Util->can('set_subname');
} or eval {
require Sub::Name;
Sub::Name->can('subname');
}
or sub { return $_[1] };
};
my %Installed;
sub _install_sub {
my $caller = shift;
my $sub_name = shift;
my $sub = shift;
my $fq_name = $caller . '::' . $sub_name;
{
## no critic (TestingAndDebugging::ProhibitNoStrict)
no strict 'refs';
*{$fq_name} = $sub_namer->( $fq_name, $sub );
}
$Installed{$caller} ||= [];
push @{ $Installed{$caller} }, $sub_name;
return;
}
sub subs_installed_into {
my $package = shift;
return @{ $Installed{$package} || [] };
}
1;
# ABSTRACT: Make validation and coercion subs from Specio types
__END__
=pod
=encoding UTF-8
=head1 NAME
Specio::Subs - Make validation and coercion subs from Specio types
=head1 VERSION
version 0.42
=head1 SYNOPSIS
use Specio::Subs qw( Specio::Library::Builtins Specio::Library::Perl My::Lib );
if ( is_PackageName($var) ) { ... }
assert_Str($var);
my $person1 = to_Person($var);
my $person2 = force_Person($var);
=head1 DESCRIPTION
This module generates a set of helpful validation and coercion subroutines for
all of the types defined in one or more libraries.
To use it, simply import C<Specio::Subs> passing a list of one or more library
names. This module will load those libraries as needed.
If any of the types in any libraries have names that do not work as part of a
Perl subroutine name, this module will throw an exception.
If you have L<Sub::Util> or L<Sub::Name> installed, one of those will be used
to name the generated subroutines.
=head1 "EXPORTS"
The following subs are created in the importing package:
=head2 is_$type($value)
This subroutine returns a boolean indicating whether or not the C<$value> is
valid for the type.
=head2 assert_$type($value)
This subroutine dies if the C<$value> is not valid for the type.
=head2 to_$type($value)
This subroutine attempts to coerce C<$value> into the given type. If it cannot
be coerced it returns the original C<$value>.
This is only created if the type has coercions.
=head2 force_$type($value)
This subroutine attempts to coerce C<$value> into the given type, and dies if
it cannot do so.
This is only created if the type has coercions.
=head1 ADDITIONAL API
=for Pod::Coverage subs_installed_into
This module has a subroutine named C<subs_installed_into>. It is not exported
but it can be called by its fully qualified name. It accepts a single
argument, a package name. It returns a list of subs that it generated and
installed in the given package, if any.
This exists to make it easy to write a type library that combines other
library and generates helper subs for export all at once.
=head1 SUPPORT
Bugs may be submitted at L<https://github.com/houseabsolute/Specio/issues>.
I am also usually active on IRC as 'autarch' on C<irc://irc.perl.org>.
=head1 SOURCE
The source code repository for Specio can be found at L<https://github.com/houseabsolute/Specio>.
=head1 AUTHOR
Dave Rolsky <autarch@urth.org>
=head1 COPYRIGHT AND LICENSE
This software is Copyright (c) 2012 - 2017 by Dave Rolsky.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)
The full text of the license can be found in the
F<LICENSE> file included with this distribution.
=cut
|