NAME
Object::Exporter - Lightweight exporting of object methods
SYNOPSIS
package MagicNumber;
use base 'Object::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!
DESCRIPTION
This module makes it much easier to make a module have a 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 'Object::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 'Object::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 'Object::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
Object::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 <david@jamesgang.com>
Most of the code and documentation was borrowed from Exporter::Lite. Exporter::Lite was written by Michael G Schwern <schwern@pobox.com>