2011年10月14日 星期五

package Arithmetic;


use Exporter;#使用匯出器Exporter


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


# Exporting the add and subtract routine
@EXPORT = qw(add subtract);#use for default Import
# Exporting the multiply and divide  routine on demand basis.
@EXPORT_OK = qw(multiply divide);#Use for specific import


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;
}



“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";

沒有留言:

張貼留言