File Coverage

blib/lib/Specio/Helpers.pm
Criterion Covered Total %
statement 65 69 94.2
branch 16 26 61.5
condition 2 12 16.6
subroutine 16 16 100.0
pod 0 2 0.0
total 99 125 79.2


line stmt bran cond sub pod time code
1             package Specio::Helpers;
2              
3 32     32   248 use strict;
  32         62  
  32         1212  
4 32     32   180 use warnings;
  32         114  
  32         1639  
5              
6 32     32   231 use Carp qw( croak );
  32         69  
  32         1880  
7 32     32   183 use Exporter 'import';
  32         126  
  32         1123  
8 32     32   18802 use overload ();
  32         63796  
  32         1618  
9              
10             our $VERSION = '0.53';
11              
12 32     32   221 use Scalar::Util qw( blessed );
  32         67  
  32         3748  
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 278     278 0 2381 require Specio::DeclaredAt;
22              
23 278         597 my $caller = shift;
24 278         500 my $types = shift;
25              
26             # XXX - check to see if their t() is something else entirely?
27             {
28             ## no critic (TestingAndDebugging::ProhibitNoStrict)
29 32     32   228 no strict 'refs';
  32         60  
  32         7431  
  278         457  
30              
31             # We used to check ->can('t') but that was wrong, since it would
32             # return if a parent class had a t() sub.
33 278 100       435 return if *{ $caller . '::t' }{CODE};
  278         2630  
34             }
35              
36             my $t = sub {
37 1107     1107   12369617 my $name = shift;
38              
39 1107 50       2778 croak 'The t subroutine requires a single non-empty string argument'
40             unless _STRINGLIKE($name);
41              
42             croak "There is no type named $name available for the $caller package"
43 1107 50       3233 unless exists $types->{$name};
44              
45 1107         1982 my $found = $types->{$name};
46              
47 1107 100       8108 return $found unless @_;
48              
49 28         101 my %p = @_;
50              
51 28 50       227 croak 'Cannot parameterize a non-parameterizable type'
52             unless $found->can('parameterize');
53              
54 28         234 return $found->parameterize(
55             declared_at => Specio::DeclaredAt->new_from_caller(1),
56             %p,
57             );
58 169         1557 };
59              
60             {
61             ## no critic (TestingAndDebugging::ProhibitNoStrict)
62 32     32   210 no strict 'refs';
  32         81  
  32         1186  
  169         353  
63 32     32   162 no warnings 'redefine';
  32         75  
  32         11302  
64 169         346 *{ $caller . '::t' } = $t;
  169         703  
65             }
66              
67 169         450 return;
68             }
69              
70             ## no critic (Subroutines::ProhibitSubroutinePrototypes, Subroutines::ProhibitExplicitReturnUndef)
71             sub _STRINGLIKE ($) {
72 1992 50   1992   4284 return $_[0] if _STRING( $_[0] );
73              
74 0 0 0     0 return $_[0]
      0        
75             if blessed $_[0]
76             && overload::Method( $_[0], q{""} )
77             && length "$_[0]";
78              
79 0         0 return undef;
80             }
81              
82             # Borrowed from Params::Util
83             sub _STRING ($) {
84 1992 50 33 1992   14242 return defined $_[0] && !ref $_[0] && length( $_[0] ) ? $_[0] : undef;
85             }
86              
87             BEGIN {
88 32 50 33 32   359 if ( $] >= 5.010 && eval { require XString; 1 } ) {
  32         14874  
  32         16076  
89 32         1383 *perlstring = \&XString::perlstring;
90             }
91             else {
92 0         0 require B;
93 0         0 *perlstring = \&B::perlstring;
94             }
95             }
96              
97             # Borrowed from Types::Standard
98             sub is_class_loaded {
99 78     78 0 19013 my $stash = do {
100             ## no critic (TestingAndDebugging::ProhibitNoStrict)
101 32     32   205 no strict 'refs';
  32         66  
  32         4961  
102 78         136 \%{ $_[0] . '::' };
  78         367  
103             };
104              
105 78 50       300 return 1 if exists $stash->{ISA};
106 78 100       228 return 1 if exists $stash->{VERSION};
107              
108 76         130 foreach my $globref ( values %{$stash} ) {
  76         230  
109             return 1
110             if ref \$globref eq 'GLOB'
111 22         244 ? *{$globref}{CODE}
112 22 50       88 : ref $globref; # const or sub ref
    100          
113             }
114              
115 55         391 return 0;
116             }
117              
118             1;
119              
120             # ABSTRACT: Helper subs for the Specio distro
121              
122             __END__
123              
124             =pod
125              
126             =encoding UTF-8
127              
128             =head1 NAME
129              
130             Specio::Helpers - Helper subs for the Specio distro
131              
132             =head1 VERSION
133              
134             version 0.53
135              
136             =head1 DESCRIPTION
137              
138             There's nothing public here.
139              
140             =for Pod::Coverage .*
141              
142             =head1 SUPPORT
143              
144             Bugs may be submitted at L<https://github.com/houseabsolute/Specio/issues>.
145              
146             =head1 SOURCE
147              
148             The source code repository for Specio can be found at L<https://github.com/houseabsolute/Specio>.
149              
150             =head1 AUTHOR
151              
152             Dave Rolsky <autarch@urth.org>
153              
154             =head1 COPYRIGHT AND LICENSE
155              
156             This software is Copyright (c) 2012 - 2025 by Dave Rolsky.
157              
158             This is free software, licensed under:
159              
160             The Artistic License 2.0 (GPL Compatible)
161              
162             The full text of the license can be found in the
163             F<LICENSE> file included with this distribution.
164              
165             =cut