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 29     29   392979 use strict;
  29         142  
  29         827  
4 29     29   164 use warnings;
  29         63  
  29         1234  
5              
6             our $VERSION = '0.47';
7              
8 29     29   159 use parent 'Exporter';
  29         65  
  29         277  
9              
10 29     29   9892 use Specio::Helpers qw( install_t_sub );
  29         71  
  29         1631  
11             use Specio::Registry
12 29     29   8698 qw( exportable_types_for_package internal_types_for_package register );
  29         75  
  29         4523  
13              
14             my %Exported;
15              
16             sub import {
17 164     164   2848 my $package = shift;
18 164         305 my $reexport = shift;
19              
20 164         403 my $caller = caller();
21              
22 164 100       678 return if $Exported{$caller}{$package};
23              
24 163         536 my $exported = exportable_types_for_package($package);
25              
26 163         353 while ( my ( $name, $type ) = each %{$exported} ) {
  2419         7640  
27 2257         5348 register( $caller, $name, $type->clone, $reexport );
28             }
29              
30             install_t_sub(
31 162         560 $caller,
32             internal_types_for_package($caller),
33             );
34              
35 162 100       1221 if ( $package->can('_also_export') ) {
36 6         34 for my $sub ( $package->_also_export ) {
37             ## no critic (TestingAndDebugging::ProhibitNoStrict)
38 29     29   628 no strict 'refs';
  29         117  
  29         3388  
39 65         105 *{ $caller . '::' . $sub } = \&{ $package . '::' . $sub };
  65         292  
  65         238  
40             }
41             }
42              
43 162         497 $Exported{$caller}{$package} = 1;
44              
45 162         51900 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.47
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 available
87             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 types
114             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 sub
121             should return a I<list> of subroutines defined in your package that should also
122             be exported. These subs will be exported unconditionally to any package that
123             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 - 2021 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