In programming, we often want to share functionality across different parts of our code. In Perl, this is commonly done through modules and exporting functions or variables. There are many ways to export, but one of the most common is using the Exporter
module. Today we will explore how to use it.
An Exporter allows you to define which functions or variables should be available to users of your module. As stated there are many ways to export functions and variables in Perl using one of the many modules available on CPAN. The most commonly used though is the Exporter
module which is included with Perl by default.
The Exporter
module provides a simple way to export functions and variables from your module to the caller's namespace. It allows you to specify which functions or variables should be exported by default, and also provides a way to export them on demand. To use the Exporter
module, you need to include it in your module and specify which functions or variables you want to export using the @EXPORT
and @EXPORT_OK
arrays. The @EXPORT
array contains the names of functions or variables that should be exported by default, while the @EXPORT_OK
array contains those that can be exported on request.
Today we will extend our previous example Module::Test
to include exporting the two functions we created and tested in our previous post. To do this lets first update our test file to test the exporting functionality. For our example we will extend in a way that requires the user to explicitly request the functions to be exported. open t/01-functionality.t
and update it as follows, extending the use_ok and removing the namespace when we call the functions:
#!perl
use 5.006;
use strict;
use warnings;
use Test::More;
plan tests => 7;
use_ok( 'Module::Test', qw/function1 function2/ );
is( function1(), 'Hello, World!', 'function1 returns the correct value' );
is_deeply( function2({ name => 'Rex', age => 30 }), { name => 'Rex', age => 30 }, 'function2 returns the correct data structure' );
eval {
function2({ name => 'Rex' });
};
like( $@, qr/age must be a number/, 'function2 throws an error when age is not provided' );
eval {
function2({ age => 30 });
};
like( $@, qr/name must be a string/, 'function2 throws an error when name is not provided' );
eval {
function2({ name => 50, age => 30 });
};
like( $@, qr/name must be a string/, 'function2 throws an error when name is not a string' );
eval {
function2({ name => "Rex", age => "thirty" });
};
like( $@, qr/age must be a number/, 'function2 throws an error when age is not a number' );
done_testing();
Now that we have our test file updated and our prove -lv t/01-functionality.t
is failing again, we can proceed to update our module to include the Exporter
functionality. Open lib/Module/Test.pm
and extend under the our $VERSION = '0.01';
line as follows:
use parent 'Exporter';
our @EXPORT_OK = qw/function1 function2/;
This code is all that is needed to enable exporting of the two functions we created, your test file should now pass when you run prove -lv t/01-functionality.t
. If you are new to Perl and following along this series then this code will be new to you, we are inheriting from the Exporter
class using use parent 'Exporter';
. We will cover inheritance in a later post but essentially this allows us to extend the functionality provided by the Exporter
module. The @EXPORT_OK
array is where we specify which functions we want to make available for export. This is defined using our
and not my
because we want this variable to be accessible globally within the module, including for the inherited module, if we were to use my
the variable would be scoped to the current file and the parent Exporter
would not be able to find it.
This concludes our exploration of exporting in Perl using the Exporter
module. I hope this post has been insightful and you have learnt the basic of exporting in Perl. While itβs possible for me to demonstrate monkey patching directly which is essentially what Exporter
does behind the scenes, weβll save that for later in your Perl journey. For now, remember that exporting is about making selected functions and variables available to users of your module, without exposing the internal details of your code. Using Exporter
lets you control exactly what is accessible, helping you maintain a clean and well-organized module interface. Also be thoughtful with naming: clear, descriptive names for exported functions and variables make your module easier to use and understand.
In the next post we will explore basic Object Oriented Programming (OOP) in Perl, which will allow us to encapsulate data and functionality within objects, making our code more organized and easier to manage. OOP is a powerful paradigm that can greatly enhance the structure of your Perl applications, and it is widely used in many Perl modules and frameworks.
Top comments (0)
Some comments may only be visible to logged-in visitors. Sign in to view all comments.