[ZZ][Perl] Export and Export_OK

本文介绍了Perl中使用Exporter模块来导出函数和变量的方法。通过示例程序详细解释了@EXPORT和@EXPORT_OK两个主要变量的作用及用法,并对比了使用与不使用Exporter时的区别。

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.

 

http://www.thegeekstuff.com/2010/06/perl-exporter-examples/

转载于:https://www.cnblogs.com/sanquanfeng/p/3370515.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值