1Class::Exporter(3) User Contributed Perl Documentation Class::Exporter(3)
2
3
4
6 Class::Exporter - Export class methods as regular subroutines
7
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
43 This module makes it much easier to make a module have a hybrid
44 object/method interface similar to the one of CGI.pm. You can take any
45 old module that has an object- oriented interface and convert it to
46 have a hybrid interface by simply adding "use base 'Class::Exporter'"
47 to your code.
48
49 This package allows you to export object methods. It supports
50 "import()", @EXPORT and @EXPORT_OK and not a whole lot else. Each
51 package into which your object methods are imported gets its own
52 instance of the object. This ensures that there are no interaction
53 effects between multiple packages 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 "use My::Module", $Foo and "bar()" 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, $Foo and "bar()" will not show up. You have
72 to ask for them. "use My::Module qw($Foo bar)".
73
75 Class::Exporter has one public method, import(), which is called
76 automatically when your modules is use()'d.
77
78 In normal usage you don't have to worry about this at all.
79
80 import
81 Some::Module->import;
82 Some::Module->import(@symbols);
83
84 Works just like "Exporter::import()" excepting it only honors
85 @Some::Module::EXPORT and @Some::Module::EXPORT_OK.
86
87 The given @symbols are exported to the current package provided
88 they are in @Some::Module::EXPORT or @Some::Module::EXPORT_OK.
89 Otherwise an exception is thrown (ie. the program dies).
90
91 If @symbols is not given, everything in @Some::Module::EXPORT is
92 exported.
93
95 '"%s" is not exported by the %s module'
96 Attempted to import a symbol which is not in @EXPORT or @EXPORT_OK.
97
98 'Can\'t export symbol: %s'
99 Attempted to import a symbol of an unknown type (ie. the leading
100 $@% salad wasn't recognized).
101
103 David James <david@jamesgang.com>
104
105 Most of the code and documentation was borrowed from Exporter::Lite.
106 Exporter::Lite was written by Michael G Schwern <schwern@pobox.com>
107
109 Exporter, Exporter::Lite, UNIVERSAL::exports
110
112 Copyright (c) 2002 David James
113 All rights reserved.
114 This program is free software; you can redistribute it and/or
115 modify it under the same terms as Perl itself.
116
117
118
119perl v5.34.0 2022-01-21 Class::Exporter(3)