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 32     32   627290 use strict;
  32         81  
  32         1194  
4 32     32   187 use warnings;
  32         56  
  32         2684  
5              
6             our $VERSION = '0.53';
7              
8 32     32   192 use parent 'Exporter';
  32         80  
  32         271  
9              
10 32     32   11644 use Specio::Helpers qw( install_t_sub );
  32         77  
  32         2293  
11             use Specio::Registry
12 32     32   11932 qw( exportable_types_for_package internal_types_for_package register );
  32         76  
  32         5929  
13              
14             my %Exported;
15              
16             sub import {
17 178     178   579969 my $package = shift;
18 178         536 my $reexport = shift;
19              
20 178         606 my $caller = caller();
21              
22 178 100       1026 return if $Exported{$caller}{$package};
23              
24 177         875 my $exported = exportable_types_for_package($package);
25              
26 177         355 while ( my ( $name, $type ) = each %{$exported} ) {
  2597         9430  
27 2421         6421 register( $caller, $name, $type->clone, $reexport );
28             }
29              
30             install_t_sub(
31 176         732 $caller,
32             internal_types_for_package($caller),
33             );
34              
35 176 100       2410 if ( $package->can('_also_export') ) {
36 9         48 for my $sub ( $package->_also_export ) {
37             ## no critic (TestingAndDebugging::ProhibitNoStrict)
38 32     32   239 no strict 'refs';
  32         2214  
  32         6112  
39 71         145 *{ $caller . '::' . $sub } = \&{ $package . '::' . $sub };
  71         328  
  71         241  
40             }
41             }
42              
43 176         616 $Exported{$caller}{$package} = 1;
44              
45 176         64012 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.53
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             =head1 SOURCE
146              
147             The source code repository for Specio can be found at L<https://github.com/houseabsolute/Specio>.
148              
149             =head1 AUTHOR
150              
151             Dave Rolsky <autarch@urth.org>
152              
153             =head1 COPYRIGHT AND LICENSE
154              
155             This software is Copyright (c) 2012 - 2025 by Dave Rolsky.
156              
157             This is free software, licensed under:
158              
159             The Artistic License 2.0 (GPL Compatible)
160              
161             The full text of the license can be found in the
162             F<LICENSE> file included with this distribution.
163              
164             =cut