来源:

http://www.cnblogs.com/itech/archive/2010/03/23/1692836.html

一 package

1) package 相当于C++中的namespace,不同的package下可以定义相同的变量和subroutines;

2)在一个pl文件中可以定义多个package,每个package有一个单独的symboltable,每个symboltable中包含了此package中的变量和subroutines;

3) package mypack; 此语句定义一个名为mypack的包,从此以后定义的所有变量和子程序的名字都存贮在该包关联的符号表中,直到遇到另一个package语句为止。默认地存储在main package中。

4)在一个包中可以引用其它包中的变量或子程序,包名和变量名用双冒号隔开,即$mypack::var。

实例:

二 module

1)module是perl的library,相当于C++中的lib或dll,是功能相近的subroutines的集合;

2)module通常存储在同名的pm文件中;

3)通常module也同时定义在一个同名的package中;

4)EXPORT用来导出subroutines,EXPORT_OK用来导出变量;

5)use mymodule;用来引用mymoudule,no mymodule来取消mymodule的引用;

实例:

USE引用模块时,如果模块名称中包含::双冒号(在MyModule.pm文件中:package MyDirectory::MyModule),该双冒号将作为路径分隔符,相当于Unix下的/或者Windows下的\。 如:
use MyDirectory::MyModule;
编译器将从@INC指定的目录下的MyDirectory子目录去寻找MyModule模块, 类似
如下路径:
C:\Perl\lib\MyDirectory
C:\Perl\ext\lib\MyDirectory
C:Perl\site\lib\MyDirectory

下的MyModule.pm文件。

三 Libraries

可以归于一般的perl文件,或module中。属于哪类,就按哪类定义,哪类使用。

可以忽略认为有这个分类

四 实例

@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.

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 ).

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

perl的package和module的更多相关文章

  1. Go中的Package和Module分析

    Package 所谓package(包)其实就是代码的一种组织管理方式,代码多了就需要放入文件,文件多了就需要归类放入文件夹,就好比我们在给电脑装软件时会进行归类安装,其实也是有意无意对电脑软件安装的 ...

  2. perl学习之:package and module

    perl的包(package)和模块(PM) ==================================包package===========================     pac ...

  3. Python package和module

    package,即包,可以把功能相近的module(模块)组织在一起,以便更好地管理.Java中也有包的概念,作用类似,是为了更好地管理类和接口.package,说白了就是个目录,不过这个目录下一定要 ...

  4. python里面的project、package、module分别是什么

    2020/5/25 1.project(项目) project 即项目,是一个很大的文件夹,里面有好多的 .py 文件. 在Spyder 中点击菜单栏 projects ----->  new ...

  5. lua module package.seeall选项

    module 与 package.seeall http://blog.codingnow.com/2006/02/lua_51_module.html 使用 module("test&qu ...

  6. perl install module as non-root user

    install to local directory. 1. cpan 初始化,不用local::lib,mannual就行,其他auto2. 修改cpan 配置文件 cpan > o conf ...

  7. Module, Package in Python

    1.To put it simple, Module是写好的一系列函数或变量,文件以.py为后缀,可以在其他Module中整体或部分引用. PS: 在Module中[结尾或开头]加入if __name ...

  8. Lua 学习之基础篇七<Lua Module,Package介绍>

    Lua 之Module介绍 包管理库提供了从 Lua 中加载模块的基础库. 只有一个导出函数直接放在全局环境中: [require]. 所有其它的部分都导出在表 package 中. require ...

  9. python 包(package)和模块(module)的创建和引入(import)

    python 包(package)和模块(module)的创建和引入(import) 名词解释 实际上,Python中的函数(Function).类(Class).模块(Module).包库(Pack ...

随机推荐

  1. 在magento里把查询语句转换称sql语句

    在magento里把查询语句转换称sql语句 $order->getSelectsql(true); order为对象.

  2. apache 安装[转]

    Apache简介         Apache HTTP Server(简称Apache)是Apache软件基金会的一个开放源码的网页服务器,可以在大多数计算机操作系统中运行,由于其多平台和安全性被广 ...

  3. Codeforces Round #366 (Div. 2)_B. Spider Man

    B. Spider Man time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  4. C# 非public的方法和属性的单元测试

    有时候我们写好的类库中,某些类的属性和方法不应该暴露出来,那么如何对这些非public的方法和属性进行单元测试? MS为我们提供了PrivateObject类,可以解决这个问题,可以去MSDN的说明文 ...

  5. Markdown - Github specific

    这篇文章的内容,必须到github的页面才能全部生效. Github specific Github allows for mistakes There are also Tables in gith ...

  6. 搭建AVL树

    #include<iostream> using namespace std; struct TreeNode { int height; //每一个结点都要保存自己的高度 int dat ...

  7. Django中使用Bootstrap

    一.在Django中引用Bootstrap模版 1.首先下载bootsrtap代码(http://v3.bootcss.com/getting-started/#download),并将下载后的文件放 ...

  8. ESFramework 4.0 进阶(04)-- 驱动力:通信引擎(下)

    在ESFramework 4.0 进阶(03)-- 驱动力:通信引擎(上)一文中,我们对ESFramework提供的每一个通信引擎的接口都做了详细了说明,这篇文章我们将继续探讨这些接口的实现类 -- ...

  9. 官方解答:Vultr VPS常见问题

    VULTR VPS配置高,价格低廉,是非常优秀的vps品牌.今天我来翻译vultr官方FAQ,相信你能找到具体答案. Q 请介绍VULTR VPS机器硬件配置 Intel CPU 3+ GHz Cor ...

  10. html的表单表格...

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...