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