File Coverage

blib/lib/Class/Exporter.pm
Criterion Covered Total %
statement 21 43 48.8
branch 8 24 33.3
condition n/a
subroutine 4 5 80.0
pod n/a
total 33 72 45.8


line stmt bran cond sub pod time code
1             package Class::Exporter;
2              
3             =head1 NAME
4              
5             Class::Exporter - Export class methods as regular subroutines
6              
7             =head1 SYNOPSIS
8              
9             package MagicNumber;
10             use base 'Class::Exporter';
11              
12             # Export object-oriented methods!
13             @EXPORT_OK = qw(magic_number);
14              
15             sub new {
16             my $class = shift;
17             bless { magic_number=>3, @_ }, $class
18             }
19            
20             sub magic_number {
21             my $self = shift;
22             @_ and $self->{magic_number} = shift;
23             $self->{magic_number}
24             }
25              
26             # Meanwhile, in another piece of code!
27             package Bar;
28             use MagicNumber; # exports magic_number
29             print magic_number; # prints 3
30             magic_number(7);
31             print magic_number; # prints 7
32            
33             # Each package gets its own instance of the object. This ensures that
34             # two packages both using your module via import semantics don't mess
35             # with each other.
36            
37             package Baz;
38             use MagicNumber; # exports magic_number
39             print magic_number; # prints 3 because this package has a different
40             # MagicNumber object than package Bar.
41            
42             =head1 DESCRIPTION
43              
44             This module makes it much easier to make a module have a hybrid object/method
45             interface similar to the one of CGI.pm. You can take any old module that has
46             an object- oriented interface and convert it to have a hybrid interface by
47             simply adding "use base 'Class::Exporter'" to your code.
48              
49             This package allows you to export object methods. It supports C,
50             C<@EXPORT> and C<@EXPORT_OK> and not a whole lot else. Each package into
51             which your object methods are imported gets its own instance of the object.
52             This ensures that there are no interaction effects between multiple packages
53             that use your object.
54              
55             Setting up a module to export its variables and functions is simple:
56              
57             package My::Module;
58             use base 'Class::Exporter';
59              
60             @EXPORT = qw($Foo bar);
61              
62             now when you C, C<$Foo> and C will show up.
63              
64             In order to make exporting optional, use @EXPORT_OK.
65              
66             package My::Module;
67             use base 'Class::Exporter';
68              
69             @EXPORT_OK = qw($Foo bar);
70              
71             when My::Module is used, C<$Foo> and C will I show up.
72             You have to ask for them. C.
73              
74             =head1 Methods
75              
76             Class::Exporter has one public method, import(), which is called
77             automatically when your modules is use()'d.
78              
79             In normal usage you don't have to worry about this at all.
80              
81             =over 4
82              
83             =item B
84              
85             Some::Module->import;
86             Some::Module->import(@symbols);
87              
88             Works just like C excepting it only honors
89             @Some::Module::EXPORT and @Some::Module::EXPORT_OK.
90              
91             The given @symbols are exported to the current package provided they
92             are in @Some::Module::EXPORT or @Some::Module::EXPORT_OK. Otherwise
93             an exception is thrown (ie. the program dies).
94              
95             If @symbols is not given, everything in @Some::Module::EXPORT is
96             exported.
97              
98             =back
99              
100             =head1 DIAGNOSTICS
101              
102             =over 4
103              
104             =item '"%s" is not exported by the %s module'
105              
106             Attempted to import a symbol which is not in @EXPORT or @EXPORT_OK.
107              
108             =item 'Can\'t export symbol: %s'
109              
110             Attempted to import a symbol of an unknown type (ie. the leading $@% salad
111             wasn't recognized).
112              
113             =back
114              
115             =head1 AUTHORS
116              
117             David James
118              
119             Most of the code and documentation was borrowed from Exporter::Lite.
120             Exporter::Lite was written by Michael G Schwern
121              
122             =head1 SEE ALSO
123              
124             L, L, L
125              
126             =head1 LICENSE
127              
128             Copyright (c) 2002 David James
129             All rights reserved.
130             This program is free software; you can redistribute it and/or
131             modify it under the same terms as Perl itself.
132            
133             =cut
134              
135             require 5.005;
136              
137             $VERSION = 0.03;
138             @EXPORT = ();
139             @EXPORT_OK = ();
140 1     1   15463 use strict 'vars'; # we're going to be doing a lot of sym refs
  1         2  
  1         634  
141              
142             sub import {
143 3     3   380 my($exporter, @imports) = @_;
144 3         14 my($caller, $file, $line) = caller;
145              
146 3 100       13 unless( @imports ) { # Default import.
147 2         4 @imports = @{$exporter.'::EXPORT'};
  2         10  
148             } else {
149             # If exporting module has an EXPORT_OK array, then exports are
150             # limited to its contents.
151 1 50       2 if( *{$exporter.'::EXPORT_OK'}{ARRAY} ) {
  1         9264  
152 0 0       0 if( @{$exporter.'::EXPORT_OK'} ) {
  0         0  
153             # This can also be cached.
154 0         0 my %ok = map { s/^&//; $_ => 1 } @{$exporter.'::EXPORT_OK'},
  0         0  
  0         0  
  0         0  
155 0         0 @{$exporter.'::EXPORT'};
156            
157 0         0 my($denied) = grep {s/^&//; !$ok{$_}} @imports;
  0         0  
  0         0  
158 0 0       0 _not_exported($denied, $exporter, $file, $line) if $denied;
159             } else { # We don't export anything.
160 0         0 _not_exported($imports[0], $exporter, $file, $line);
161             }
162             }
163             }
164              
165 3 100       28 @imports and _export($caller, $exporter, @imports);
166             }
167              
168              
169             sub _export {
170 2     2   7 my($caller, $exporter, @imports) = @_;
171              
172 2 50       21 $exporter->can("new") or do {
173 0         0 require Carp;
174 0         0 Carp::croak(
175             "Class must have 'new' method in order to export class methods"
176             );
177             };
178              
179             # Declare an individual instance for each module that uses us.
180 2         13 my $instance = $exporter->new(exports=>[\@imports]);
181              
182             # Stole this from Exporter::Heavy. I'm sure it can be written better
183             # but I'm lazy at the moment.
184 2         20 foreach my $sym (@imports) {
185 2         3 my $type = "&";
186 2 50       10 $sym =~ s/^(\W)// and $type = $1;
187              
188 2         6 my $export_sym = $exporter.'::'.$sym;
189 2         2330 *{$caller.'::'.$sym} =
190 4     4   313 $type eq '&' ? sub { $instance->$sym(@_) } :
191 0           $type eq '$' ? \${$export_sym} :
  0            
192 0           $type eq '@' ? \@{$export_sym} :
193 0           $type eq '%' ? \%{$export_sym} :
194             $type eq '*' ? *{$export_sym} :
195 2 0       14 do { require Carp; Carp::croak("Can't export symbol: $type$sym") };
  0 0          
  0 0          
    0          
    50          
196             }
197             }
198              
199             sub _not_exported {
200 0     0     my($thing, $exporter, $file, $line) = @_;
201 0           die sprintf qq|"%s" is not exported by the %s module at %s line %d\n|,
202             $thing, $exporter, $file, $line;
203             }
204              
205             1;
206