File Coverage

blib/lib/Specio/Exporter.pm
Criterion Covered Total %
statement 34 34 100.0
branch 4 4 100.0
condition n/a
subroutine 7 7 100.0
pod n/a
total 45 45 100.0


line stmt bran cond sub pod time code
1             package Specio::Exporter;
2              
3 28     28   507430 use strict;
  28         102  
  28         718  
4 28     28   134 use warnings;
  28         47  
  28         1024  
5              
6             our $VERSION = '0.46';
7              
8 28     28   134 use parent 'Exporter';
  28         55  
  28         185  
9              
10 28     28   8474 use Specio::Helpers qw( install_t_sub );
  28         56  
  28         1460  
11             use Specio::Registry
12 28     28   7281 qw( exportable_types_for_package internal_types_for_package register );
  28         58  
  28         3899  
13              
14             my %Exported;
15              
16             sub import {
17 155     155   2347 my $package = shift;
18 155         242 my $reexport = shift;
19              
20 155         300 my $caller = caller();
21              
22 155 100       564 return if $Exported{$caller}{$package};
23              
24 154         493 my $exported = exportable_types_for_package($package);
25              
26 154         294 while ( my ( $name, $type ) = each %{$exported} ) {
  2255         6271  
27 2102         4370 register( $caller, $name, $type->clone, $reexport );
28             }
29              
30             install_t_sub(
31 153         492 $caller,
32             internal_types_for_package($caller),
33             );
34              
35 153 100       1049 if ( $package->can('_also_export') ) {
36 5         23 for my $sub ( $package->_also_export ) {
37             ## no critic (TestingAndDebugging::ProhibitNoStrict)
38 28     28   248 no strict 'refs';
  28         56  
  28         2902  
39 63         84 *{ $caller . '::' . $sub } = \&{ $package . '::' . $sub };
  63         258  
  63         138  
40             }
41             }
42              
43 153         418 $Exported{$caller}{$package} = 1;
44              
45 153         45434 return;
46             }
47              
48             1;
49              
50             # ABSTRACT: Base class for type libraries
51              
52             __END__
53              
54             =pod
55              
56             =encoding UTF-8
57              
58             =head1 NAME
59              
60             Specio::Exporter - Base class for type libraries
61              
62             =head1 VERSION
63              
64             version 0.46
65              
66             =head1 SYNOPSIS
67              
68             package MyApp::Type::Library;
69              
70             use parent 'Specio::Exporter';
71              
72             use Specio::Declare;
73              
74             declare( ... );
75              
76             # more types here
77              
78             package MyApp::Foo;
79              
80             use MyApp::Type::Library
81              
82             =head1 DESCRIPTION
83              
84             Inheriting from this package makes your package a type exporter. By default,
85             types defined in a package are never visible outside of the package. When you
86             inherit from this package, all the types you define internally become
87             available via exports.
88              
89             The exported types are available through the importing package's C<t>
90             subroutine.
91              
92             By default, types your package imports are not re-exported:
93              
94             package MyApp::Type::Library;
95              
96             use parent 'Specio::Exporter';
97              
98             use Specio::Declare;
99             use Specio::Library::Builtins;
100              
101             In this case, the types provided by L<Specio::Library::Builtins> are not
102             exported to packages which C<use MyApp::Type::Library>.
103              
104             You can explicitly ask for types to be re-exported:
105              
106             package MyApp::Type::Library;
107              
108             use parent 'Specio::Exporter';
109              
110             use Specio::Declare;
111             use Specio::Library::Builtins -reexport;
112              
113             In this case, packages which C<use MyApp::Type::Library> will get all the
114             types from L<Specio::Library::Builtins> as well as any types defined in
115             C<MyApp::Type::Library>.
116              
117             =head1 ADDITIONAL EXPORTS
118              
119             If you want to export some additional subroutines from a package which has
120             C<Specio::Exporter> as its parent, define a sub named C<_also_export>. This
121             sub should return a I<list> of subroutines defined in your package that should
122             also be exported. These subs will be exported unconditionally to any package
123             that uses your package.
124              
125             =head1 COMBINING LIBRARIES WITH L<Specio::Subs>
126              
127             You can combine loading libraries with subroutine generation using
128             L<Specio::Subs> by using C<_also_export> and
129             C<Specio::Subs::subs_installed_into>:
130              
131             package My::Library;
132              
133             use My::Library::Internal -reexport;
134             use Specio::Library::Builtins -reexport;
135             use Specio::Subs qw( My::Library::Internal Specio::Library::Builtins );
136              
137             sub _also_export {
138             return Specio::Subs::subs_installed_into(__PACKAGE__);
139             }
140              
141             =head1 SUPPORT
142              
143             Bugs may be submitted at L<https://github.com/houseabsolute/Specio/issues>.
144              
145             I am also usually active on IRC as 'autarch' on C<irc://irc.perl.org>.
146              
147             =head1 SOURCE
148              
149             The source code repository for Specio can be found at L<https://github.com/houseabsolute/Specio>.
150              
151             =head1 AUTHOR
152              
153             Dave Rolsky <autarch@urth.org>
154              
155             =head1 COPYRIGHT AND LICENSE
156              
157             This software is Copyright (c) 2012 - 2020 by Dave Rolsky.
158              
159             This is free software, licensed under:
160              
161             The Artistic License 2.0 (GPL Compatible)
162              
163             The full text of the license can be found in the
164             F<LICENSE> file included with this distribution.
165              
166             =cut