Sometimes, you may want to
selectively import symbols into your namespace, just for typing
efficiency. For example, you might want to say
sqrt
instead of math::sqrt
or
deposit
instead of
BankAccount::deposit
. The
use
statement allows you to specify an optional list of function names to
be imported:
use BankAccount ('withdraw', 'deposit'); withdraw(); # Can now call function without fully qualifying it.
For its part, the module has to be ready to export these names (and
only them) to whoever use
s it. It should also have
a policy for what it should do if the user does not specify a list at
all. Both these tasks are handled for you by a standard module called
Exporter. The BankAccount package can be
implemented as shown next:
package BankAccount; use Exporter; @ISA = ('Exporter'); # Inherit from Exporter @EXPORT_OK = ('withdraw', 'deposit'); sub deposit { .... } sub withdraw { .... }
This code loads the Exporter module and arranges to inherit from that
module, using the @ISA
array. For now, take it on
faith that this works; we will study inheritance shortly. The
@EXPORT_OK
array states which symbols are fine to
export. The user of this module can in turn specify a list of one or
more symbols specified in @EXPORT_OK
to the
use
statement. If the user says,
use BankAccount ('deposit');
the deposit
function can be called without fully
qualifying the name, in contrast to withdraw()
. To tell the Exporter module not to export any symbols into your namespace, leave the list ...
No credit card required