File Coverage

blib/lib/Specio/Helpers.pm
Criterion Covered Total %
statement 60 64 93.7
branch 16 26 61.5
condition 2 12 16.6
subroutine 15 15 100.0
pod 0 2 0.0
total 93 119 78.1


line stmt bran cond sub pod time code
1             package Specio::Helpers;
2              
3 28     28   167 use strict;
  28         52  
  28         696  
4 28     28   117 use warnings;
  28         44  
  28         659  
5              
6 28     28   124 use Carp qw( croak );
  28         45  
  28         1318  
7 28     28   146 use Exporter 'import';
  28         51  
  28         781  
8 28     28   28186 use overload ();
  28         22946  
  28         1046  
9              
10             our $VERSION = '0.46';
11              
12 28     28   169 use Scalar::Util qw( blessed );
  28         49  
  28         6050  
13              
14             our @EXPORT_OK = qw( install_t_sub is_class_loaded perlstring _STRINGLIKE );
15              
16             sub install_t_sub {
17              
18             # Specio::DeclaredAt use Specio::OO, which in turn uses
19             # Specio::Helpers. If we load this with "use" we get a cirular require and
20             # a big mess.
21 245     245 0 1441 require Specio::DeclaredAt;
22              
23 245         441 my $caller = shift;
24 245         364 my $types = shift;
25              
26             # XXX - check to see if their t() is something else entirely?
27 245 100       2673 return if $caller->can('t');
28              
29             my $t = sub {
30 1025     1025   389176 my $name = shift;
31              
32 1025 50       2069 croak 'The t subroutine requires a single non-empty string argument'
33             unless _STRINGLIKE($name);
34              
35             croak "There is no type named $name available for the $caller package"
36 1025 50       2441 unless exists $types->{$name};
37              
38 1025         1666 my $found = $types->{$name};
39              
40 1025 100       6243 return $found unless @_;
41              
42 19         59 my %p = @_;
43              
44 19 50       117 croak 'Cannot parameterize a non-parameterizable type'
45             unless $found->can('parameterize');
46              
47 19         113 return $found->parameterize(
48             declared_at => Specio::DeclaredAt->new_from_caller(1),
49             %p,
50             );
51 146         1099 };
52              
53             {
54             ## no critic (TestingAndDebugging::ProhibitNoStrict)
55 28     28   191 no strict 'refs';
  28         52  
  28         1000  
  146         288  
56 28     28   163 no warnings 'redefine';
  28         51  
  28         7023  
57 146         217 *{ $caller . '::t' } = $t;
  146         566  
58             }
59              
60 146         383 return;
61             }
62              
63             ## no critic (Subroutines::ProhibitSubroutinePrototypes, Subroutines::ProhibitExplicitReturnUndef)
64             sub _STRINGLIKE ($) {
65 1826 50   1826   3143 return $_[0] if _STRING( $_[0] );
66              
67 0 0 0     0 return $_[0]
      0        
68             if blessed $_[0]
69             && overload::Method( $_[0], q{""} )
70             && length "$_[0]";
71              
72 0         0 return undef;
73             }
74              
75             # Borrowed from Params::Util
76             sub _STRING ($) {
77 1826 50 33 1826   10330 return defined $_[0] && !ref $_[0] && length( $_[0] ) ? $_[0] : undef;
78             }
79              
80             BEGIN {
81 28 50 33 28   266 if ( $] >= 5.010 && eval { require XString; 1 } ) {
  28         11212  
  28         10165  
82 28         893 *perlstring = \&XString::perlstring;
83             }
84             else {
85 0         0 require B;
86 0         0 *perlstring = \&B::perlstring;
87             }
88             }
89              
90             # Borrowed from Types::Standard
91             sub is_class_loaded {
92 77     77 0 12402 my $stash = do {
93             ## no critic (TestingAndDebugging::ProhibitNoStrict)
94 28     28   166 no strict 'refs';
  28         48  
  28         3000  
95 77         109 \%{ $_[0] . '::' };
  77         479  
96             };
97              
98 77 50       220 return 1 if exists $stash->{ISA};
99 77 100       166 return 1 if exists $stash->{VERSION};
100              
101 76         105 foreach my $globref ( values %{$stash} ) {
  76         206  
102             return 1
103             if ref \$globref eq 'GLOB'
104 25         238 ? *{$globref}{CODE}
105 25 50       73 : ref $globref; # const or sub ref
    100          
106             }
107              
108 55         330 return 0;
109             }
110              
111             1;
112              
113             # ABSTRACT: Helper subs for the Specio distro
114              
115             __END__
116              
117             =pod
118              
119             =encoding UTF-8
120              
121             =head1 NAME
122              
123             Specio::Helpers - Helper subs for the Specio distro
124              
125             =head1 VERSION
126              
127             version 0.46
128              
129             =head1 DESCRIPTION
130              
131             There's nothing public here.
132              
133             =for Pod::Coverage .*
134              
135             =head1 SUPPORT
136              
137             Bugs may be submitted at L<https://github.com/houseabsolute/Specio/issues>.
138              
139             I am also usually active on IRC as 'autarch' on C<irc://irc.perl.org>.
140              
141             =head1 SOURCE
142              
143             The source code repository for Specio can be found at L<https://github.com/houseabsolute/Specio>.
144              
145             =head1 AUTHOR
146              
147             Dave Rolsky <autarch@urth.org>
148              
149             =head1 COPYRIGHT AND LICENSE
150              
151             This software is Copyright (c) 2012 - 2020 by Dave Rolsky.
152              
153             This is free software, licensed under:
154              
155             The Artistic License 2.0 (GPL Compatible)
156              
157             The full text of the license can be found in the
158             F<LICENSE> file included with this distribution.
159              
160             =cut