NAME
Class::Exporter - Export class methods as regular subroutines
SYNOPSIS
package MagicNumber;
use base 'Class::Exporter';
# Export object-oriented methods!
@EXPORT_OK = qw(magic_number);
sub new {
my $class = shift;
bless { magic_number=>3, @_ }, $class
}
sub magic_number {
my $self = shift;
@_ and $self->{magic_number} = shift;
$self->{magic_number}
}
# Meanwhile, in another piece of code!
package Bar;
use MagicNumber; # exports magic_number
print magic_number; # prints 3
magic_number(7);
print magic_number; # prints 7
# Each package gets its own instance of the object. This ensures that
# two packages both using your module via import semantics don't mess
# with each other.
package Baz;
use MagicNumber; # exports magic_number
print magic_number; # prints 3 because this package has a different
# MagicNumber object than package Bar.
DESCRIPTION
This module makes it much easier to make a module have a hybrid object/method interface similar to the one of CGI.pm. You can take any old module that has an object- oriented interface and convert it to have a hybrid interface by simply adding "use base 'Class::Exporter'" to your code.
This package allows you to export object methods. It supports import()
, @EXPORT
and @EXPORT_OK
and not a whole lot else. Each package into which your object methods are imported gets its own instance of the object. This ensures that there are no interaction effects between multiple packages that use your object.
Setting up a module to export its variables and functions is simple:
package My::Module;
use base 'Class::Exporter';
@EXPORT = qw($Foo bar);
now when you use My::Module
, $Foo
and bar()
will show up.
In order to make exporting optional, use @EXPORT_OK.
package My::Module;
use base 'Class::Exporter';
@EXPORT_OK = qw($Foo bar);
when My::Module is used, $Foo
and bar()
will not show up. You have to ask for them. use My::Module qw($Foo bar)
.
Methods
Class::Exporter has one public method, import(), which is called automatically when your modules is use()'d.
In normal usage you don't have to worry about this at all.
- import
-
Some::Module->import; Some::Module->import(@symbols);
Works just like
Exporter::import()
excepting it only honors @Some::Module::EXPORT and @Some::Module::EXPORT_OK.The given @symbols are exported to the current package provided they are in @Some::Module::EXPORT or @Some::Module::EXPORT_OK. Otherwise an exception is thrown (ie. the program dies).
If @symbols is not given, everything in @Some::Module::EXPORT is exported.
DIAGNOSTICS
- '"%s" is not exported by the %s module'
-
Attempted to import a symbol which is not in @EXPORT or @EXPORT_OK.
- 'Can\'t export symbol: %s'
-
Attempted to import a symbol of an unknown type (ie. the leading $@% salad wasn't recognized).
AUTHORS
David James <[email protected]>
Most of the code and documentation was borrowed from Exporter::Lite. Exporter::Lite was written by Michael G Schwern <[email protected]>
SEE ALSO
Exporter, Exporter::Lite, UNIVERSAL::exports
LICENSE
Copyright (c) 2002 David James
All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.