在 Perl 中,使用模块比使用文件更好吗? [英] In Perl, is it better to use a module than to require a file?

查看:18
本文介绍了在 Perl 中,使用模块比使用文件更好吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

另一个问题我在思考代码重用的不同方法:use vs. require vs. do

Another question got me thinking about different methods of code reuse: use vs. require vs. do

我在这里看到很多帖子,问题集中在使用 require 来加载和执行代码.在我看来,这显然是一种不好的做法,但我还没有找到有关该问题的任何好的资源,可以让人们指出.

I see a lot of posts here where the question centers around the use of require to load and execute code. This seems to me to be an obvious bad practice, but I haven't found any good resources on the issue that I can point people to.

perlfaq8 涵盖了 userequire 之间的区别,但它没有提供关于偏好的任何建议(截至 5.10--in 5.8.8 有一些支持使用的快速建议).

perlfaq8 covers the difference between use and require, but it doesn't offer any advice in regard to preference (as of 5.10--in 5.8.8 there is a quick bit of advice in favor of use).

这个话题似乎缺乏讨论.我有几个问题希望大家讨论:

This topic seems to suffer from a lack of discussion. I have a few questions I'd like to see discussed:

  1. Perl 中代码重用的首选方法是什么?
    • use ModuleName;
    • require ModuleName;
    • 需要'file.pl';
    • 做'file.pl';

推荐答案

标准做法是大部分时间使用use,偶尔requiredo 很少.

Standard practice is to use use most of the time, require occasionally, and do rarely.

do 'file' 会将 file 作为 Perl 脚本执行.这几乎就像对文件内容调用 eval 一样;如果您多次do 同一个文件(例如在循环中),它每次都会被解析和评估,这不太可能是您想要的.doeval 的区别在于 do 看不到封闭范围内的词法变量,这使得它更安全.do 有时对处理以 Perl 代码形式编写的配置文件等简单任务很有用.

do 'file' will execute file as a Perl script. It's almost like calling eval on the contents of the file; if you do the same file multiple times (e.g. in a loop) it will be parsed and evaluated each time which is unlikely to be what you want. The difference between do and eval is that do can't see lexical variables in the enclosing scope, which makes it safer. do is occasionally useful for simple tasks like processing a configuration file that's written in the form of Perl code.

require 'file'do 'file' 类似,不同之处在于它只会解析任何特定文件一次,并且在出现问题时会引发异常.(例如无法找到文件,它包含语法错误等)自动错误检查使其成为 do 'file' 的良好替代品,但它仍然只适用于相同的简单用途.

require 'file' is like do 'file' except that it will only parse any particular file one time and will raise an exception if something goes wrong. (e.g. the file can't be found, it contains a syntax error, etc.) The automatic error checking makes it a good replacement for do 'file' but it's still only suited for the same simple uses.

do 'file'require 'file' 表单是很久以前 *.pl 文件扩展名意味着Perl 库"的遗留物.在 Perl 中重用代码的现代方法是将其组织成模块.将某些东西称为模块"而不是库"只是语义,但这些词在 Perl 文化中的含义截然不同.库只是子程序的集合;一个模块提供了一个命名空间,使其更适合重用.

The do 'file' and require 'file' forms are carryovers from days long past when the *.pl file extension meant "Perl Library." The modern way of reusing code in Perl is to organize it into modules. Calling something a "module" instead of a "library" is just semantics, but the words mean distinctly different things in Perl culture. A library is just a collection of subroutines; a module provides a namespace, making it far more suitable for reuse.

use Module 是使用模块代码的正常方式.请注意,Module 是作为裸字的包名称,而不是包含文件名的带引号的字符串.Perl 为您处理从包名到文件名的转换.use 语句在编译时发生,如果失败则抛出异常.这意味着如果您的代码所依赖的模块不可用或无法加载,错误将立即显现.此外,use 会自动调用模块的 import() 方法,如果它有一个可以节省你一点输入的方法.

use Module is the normal way of using code from a module. Note that Module is the package name as a bareword and not a quoted string containing a file name. Perl handles the translation from a package name to a file name for you. use statements happen at compile time and throw an exception if they fail. This means that if a module your code depends on isn't available or fails to load the error will be apparent immediately. Additionally, use automatically calls the import() method of the module if it has one which can save you a little typing.

require Moduleuse Module 类似,只是它在运行时发生并且不会自动调用模块的 import() 方法.通常,您希望使用 use 尽早且可预测地失败,但有时 require 更好.例如,require 可用于延迟加载仅偶尔需要的大型模块或使模块可选.(即,如果模块可用,则使用该模块,但如果不可用,则退回到其他东西或减少功能.)

require Module is like use Module except that it happens at runtime and does not automatically call the module's import() method. Normally you want to use use to fail early and predictably, but sometimes require is better. For example, require can be used to delay the loading of large modules which are only occasionally required or to make a module optional. (i.e. use the module if it's available but fall back on something else or reduce functionality if it isn't.)

严格来说,require Modulerequire 'file' 之间的唯一区别是第一种形式触发了从包名称(如 Foo)的自动转换::Bar 到像 Foo/Bar.pm 这样的文件名,而后一种形式需要一个文件名开头.不过,按照惯例,第一种形式用于加载模块,而第二种形式用于加载库.

Strictly speaking, the only difference between require Module and require 'file' is that the first form triggers the automatic translation from a package name like Foo::Bar to a file name like Foo/Bar.pm while the latter form expects a filename to start with. By convention, though, the first form is used for loading modules while the second form is used for loading libraries.

这篇关于在 Perl 中,使用模块比使用文件更好吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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