perl 中的命名空间、包和模块有什么区别? [英] what is difference between namespace, package and module in perl?

查看:70
本文介绍了perl 中的命名空间、包和模块有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

命名空间或包是否相同?我在我们只有包的地方使用 Perl.我知道还有其他编程语言也包含模块.

Namespace or package are same? I use Perl where we only have packages. I know there are other programming languages that also include modules.

有什么区别?

推荐答案

Namespace 是一个通用计算术语,表示一组不同标识符的容器.同一个标识符可以独立出现在不同的命名空间中并引用不同的对象,而一个明确标识一个对象的完全限定标识符由命名空间加上标识符组成.

Namespace is a general computing term meaning a container for a distinct set of identifiers. The same identifier can appear independently in different namespaces and refer to different objects, and a fully-qualified identifier which unambiguously identifies an object consists of the namespace plus the identifier.

Perl 使用 package 关键字实现命名空间.

Perl implements namespaces using the package keyword.

Perl 模块 完全不同.它是一段 Perl 代码,可以使用 use 关键字合并到任何程序中.文件名应该以 .pm 结尾 - 对于 Perl Module - 并且它包含的代码应该有一个 package 语句使用与文件名等价的包名,包括其路径.例如,在名为 My/Useful/Module.pm 的文件中编写的模块应该有一个 package 语句,如 package My::Useful::Module.

A Perl module is a different thing altogether. It is a piece of Perl code that can be incorporated into any program with the use keyword. The filename should end with .pm - for Perl Module - and the code it contains should have a package statement using a package name that is equivalent to the file's name, including its path. For instance, a module written in a file called My/Useful/Module.pm should have a package statement like package My::Useful::Module.

您可能想到的是,它也是一个通用的计算术语,这次是指一种面向对象的数据.Perl 使用它的包作为类名,面向对象的模块将有一个 constructor 子例程 - 通常称为 new - 它将返回对已blessed 使其以面向对象的方式运行.绝不是所有的 Perl 模块都是面向对象的:有些可以是简单的子程序库.

What you may have been thinking of is a class which, again, is a general computing term, this time meaning a type of object-oriented data. Perl uses its packages as class names, and an object-oriented module will have a constructor subroutine - usually called new - that will return a reference to data that has been blessed to make it behave in an object-oriented fashion. By no means all Perl modules are object-oriented ones: some can be simple libraries of subroutines.

这篇关于perl 中的命名空间、包和模块有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆