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

查看:72
本文介绍了在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开始,在

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;
    • require 'file.pl';
    • do 'file.pl';
  1. What is the preferred method of code reuse in Perl?
    • use ModuleName;
    • require ModuleName;
    • require 'file.pl';
    • do 'file.pl';

推荐答案

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

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天全站免登陆