≡ Menu

Perl Exporter Tutorial with Examples – @EXPORT and @EXPORT_OK

Export allows to export the functions and variables of modules to user’s namespace using the standard import method. This way, we don’t need to create the objects for the modules to access it’s members.

@EXPORT and @EXPORT_OK are the two main variables used during export operation.

@EXPORT contains list of symbols (subroutines and variables) of the module to be exported into the caller namespace.

@EXPORT_OK does export of symbols on demand basis.

Sample program

Let us use the following sample program to understand Perl exporter.

package Arithmetic;

use Exporter;

# base class of this(Arithmetic) module
@ISA = qw(Exporter);

# Exporting the add and subtract routine
@EXPORT = qw(add subtract);
# Exporting the multiply and divide  routine on demand basis.
@EXPORT_OK = qw(multiply divide);

sub add
{
my ($no1,$no2) = @_;
my $result;
$result = $no1+$no2;
return $result;
}

sub subtract
{
my ($no1,$no2) = @_;
my $result;
$result = $no1-$no2;
return $result;

}

sub multiply
{
my ($no1,$no2) = @_;
my $result;
$result = $no1*$no2;
return $result;
}

sub divide
{
my ($no1,$no2) = @_;
my $result;
$result = $no1/$no2;
return $result;

}

In the above example, we defined the arithmetic module with four functions. By default add() and subtract() functions are exported to the user’s/caller’s namespace.

“use Arithmetic” statement imports the subroutines from Arithmetic module that are exported by default.

“use Arithmetic qw(multiply divide)” indicates that these two routines gets exported only when it is specifically requested as shown in the following code snippet.

#! /usr/bin/perl

use strict;
use warnings;

use Arithmetic;
use Arithmetic qw(multiply divide);

print add(1,2),"\n";
print multiply(1,2),"\n";

As we seen above, in the main program we used the Arithmetic module with default import ( add and subtract ) and on-demand import ( multiply and divide ).

How Imports Gets Done

Without Exporter, to import functions from Arithmetic module into the main package, we can write a code-snippet like the following to import some of the functions.

sub import {
no strict 'refs';
for (qw(add subtract multiply )) {
*{"main::$_"} = \&$_;
}
}

The code in the main package/program are as follows,

#!/usr/bin/perl
use Arithmetic;
# import() subroutine in Arithmetic module gets invoked automatically.
print add(1,2);

In the above code-snippet, from the current package ( Arithmetic ), these routines ( add, subtract, multiply ) gets imported into the main package. This provides limited functionality. i.e In case if the use is invoked from packages other than main, this wont work properly. But in the Perl Exporter module, we don’t have this limitation.

Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • Sathish Kumar March 23, 2011, 9:08 pm

    Hi ,

    I am really happy and satisfied to give the comment about this site.
    So far I am wondering about the EXPORT and EXPORT_OK in the perl. but after reading this article in your site. really get to know how the exporter is working in the PERL.

  • Ananda June 8, 2011, 7:12 am

    Hey i was really searching for a simple material to understand these concepts.
    I’m very happy that i found the right one. Clear and simple explanation.
    Thanks a lot.

  • Nagesh Palathya June 16, 2011, 4:00 am

    Thanks, explanation is very clear.

  • Nagaraj Anantha October 6, 2011, 12:13 am

    Thanks for understand this concept in very easy way.

  • Sidraj November 19, 2011, 6:46 am

    I am really happy and satisfied to give the comment about this site.
    So far I was wondering about the EXPORT and EXPORT_OK in the perl. but after reading this article in your site. really get to know how the exporter is working in the PERL.

  • Santosh June 18, 2012, 7:58 am

    Hi,
    Could please help to explain me this code-snippet. Thanks in advance

    sub import {
    no strict ‘refs’;
    for (qw(add subtract multiply )) {
    *{“main::$_”} = \&$_;
    }
    }

  • Shobit November 6, 2012, 11:01 pm

    Very good example to understand this concept.

  • Bharat April 16, 2013, 2:53 am

    Thank you very much.

  • Abhishek April 29, 2013, 2:57 pm

    Thanks for very good article. Please keep publishing this type of article.

  • Atul Bajpai July 2, 2015, 4:57 am

    Thank you so much for this post Its really very good and simple to understand. Request you to please keep posted such type of articles in future.

  • sekhar August 10, 2015, 12:02 pm

    It is awesome explanation . I refered so many books websites no where they clearly explained what exporter will do . Thank you very much . Thanks a lotttttttttt!! 🙂