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 29     29   292 use strict;
  29         69  
  29         897  
4 29     29   174 use warnings;
  29         56  
  29         838  
5              
6 29     29   141 use Carp qw( croak );
  29         51  
  29         1428  
7 29     29   163 use Exporter 'import';
  29         62  
  29         941  
8 29     29   34424 use overload ();
  29         28424  
  29         1197  
9              
10             our $VERSION = '0.47';
11              
12 29     29   202 use Scalar::Util qw( blessed );
  29         77  
  29         7332  
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 257     257 0 1728 require Specio::DeclaredAt;
22              
23 257         569 my $caller = shift;
24 257         400 my $types = shift;
25              
26             # XXX - check to see if their t() is something else entirely?
27 257 100       3543 return if $caller->can('t');
28              
29             my $t = sub {
30 1041     1041   96612 my $name = shift;
31              
32 1041 50       2418 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 1041 50       2841 unless exists $types->{$name};
37              
38 1041         1822 my $found = $types->{$name};
39              
40 1041 100       7532 return $found unless @_;
41              
42 25         105 my %p = @_;
43              
44 25 50       177 croak 'Cannot parameterize a non-parameterizable type'
45             unless $found->can('parameterize');
46              
47 25         153 return $found->parameterize(
48             declared_at => Specio::DeclaredAt->new_from_caller(1),
49             %p,
50             );
51 155         1143 };
52              
53             {
54             ## no critic (TestingAndDebugging::ProhibitNoStrict)
55 29     29   224 no strict 'refs';
  29         63  
  29         1180  
  155         333  
56 29     29   178 no warnings 'redefine';
  29         79  
  29         8750  
57 155         258 *{ $caller . '::t' } = $t;
  155         646  
58             }
59              
60 155         439 return;
61             }
62              
63             ## no critic (Subroutines::ProhibitSubroutinePrototypes, Subroutines::ProhibitExplicitReturnUndef)
64             sub _STRINGLIKE ($) {
65 1866 50   1866   3613 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 1866 50 33 1866   12200 return defined $_[0] && !ref $_[0] && length( $_[0] ) ? $_[0] : undef;
78             }
79              
80             BEGIN {
81 29 50 33 29   307 if ( $] >= 5.010 && eval { require XString; 1 } ) {
  29         13896  
  29         11733  
82 29         1060 *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 78     78 0 9646 my $stash = do {
93             ## no critic (TestingAndDebugging::ProhibitNoStrict)
94 29     29   203 no strict 'refs';
  29         57  
  29         3477  
95 78         122 \%{ $_[0] . '::' };
  78         398  
96             };
97              
98 78 50       474 return 1 if exists $stash->{ISA};
99 78 100       220 return 1 if exists $stash->{VERSION};
100              
101 76         121 foreach my $globref ( values %{$stash} ) {
  76         199  
102             return 1
103             if ref \$globref eq 'GLOB'
104 23         246 ? *{$globref}{CODE}
105 23 50       76 : ref $globref; # const or sub ref
    100          
106             }
107              
108 55         356 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.47
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 - 2021 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