Learning Perl - Exporting


Learning Perl - Exporting


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();
Enter fullscreen mode Exit fullscreen mode

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/;
Enter fullscreen mode Exit fullscreen mode

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.

Related Blogs

Learning Perl – Introduction
Perl has long been known as the “duct tape of the Internet,” or "the Swiss Army chainsaw of scripting...
Learning Perl - Variables
I will attempt to explain things in this post in a way that is easy to understand, even for those who...
Learning Perl - Arrays
As stated in the previous post, Perl has three types of variables: scalars, arrays and hashes. Today...
Learning Perl - Hashes
In the last post we covered the basics of arrays, today we will look at hashes in more detail. What...
Learning Perl - Conditional Statements
So far we have covered basic variables in Perl, today we are going to look at how to use these...
Learning Perl - Loops and Iteration
In previous posts, we explored variables, arrays, hashes, and conditional statements in Perl. Now...
Learning Perl - Scalars
Before moving onto more complex topics lets come back to how we represent data in Perl. The most...
Learning Perl - References
In the last post we learnt how to create a reference to a scalar, an array, and a hash. In this post,...
Learning Perl - Ternary Operators
In a previous post, we learned about conditional statements in Perl. The ternary operator is an...
Learning Perl - Subroutines
Subroutines are one of the most important building blocks in programming. They allow you to organise...
Learning Perl - Regular Expressions
Regular expressions also known as regex or regexp, are a powerful way to match patterns in text. Most...
Learning Perl - Modules
A module in Perl is a reusable piece of code that can be included in your scripts to provide...
Learning Perl - CPAN
In the last post I showed you how to create a new module and how to use it in your code. In this post...
Learning Perl - Plain Old Documentation
When you write and program in any language it is good practice to document your code. Each language...
Learning Perl - Testing
In this post we will look at how to test Perl code using the Test::More module. Like documentation,...
Learning Perl - Object Orientation
Object-Oriented Programming (OOP) is a widely used programming paradigm that enables the creation of...
Learning Perl - Inheritance
In the last post we discussed Object Oriented Programming in Perl, focusing on the basics of creating...
Learning Perl - File Handles
In programming file processing is a key skill to master. Files are essential for storing data,...
Learning Perl - Prototypes
Today we are going to discuss Perl subroutine prototypes, which are a way to enforce a certain...
Learning Perl - Overloading Operators
In the last post we investigated prototype subroutines in Perl. In this post, we will look at...