为什么每个perl包的末尾都有1个?

时间:2021-08-30 12:17:38

If you forget the 1 at the end of a package, Perl tells you "The package didn't return a true value". Well, if it knows you forgot it, why not just put it there for you?

如果你忘记了软件包末尾的1,Perl会告诉你“软件包没有返回真值”。好吧,如果它知道你忘了它,为什么不把它放在那里呢?

4 个解决方案

#1


36  

Because Perl modules are required to return a value to signal if the require directive must succeed (true value returned) or fail (false value returned; this can make sense if the module failed to initialize for some reason).

因为如果require指令必须成功(返回true值)或失败(返回false值,则需要Perl模块返回一个值来发出信号;如果模块由于某种原因未能初始化,这可能是有意义的)。

If you don't return anything, the interpreter cannot know if the require must succeed or fail; at the same time, since it's easy to forget to put the true value at the end of the package, it suggests the "common fix" for this error: add a true value as a return.

如果您不返回任何内容,则口译员无法知道要求是否必须成功或失败;同时,由于很容易忘记将真值放在包的末尾,它建议对此错误进行“常见修复”:添加一个真值作为返回。

For some other info/folklore about the modules return value have a look at this question.

有关模块返回值的其他信息/民间传说,请查看此问题。

#2


5  

A package can return a false value if it fails to initialize, for example if it couldn't find a required data file or external library. This way it fails cleanly at load time (and this failure can even be tested for) rather than unpredictably later.

如果包无法初始化,则包可以返回false值,例如,如果它找不到所需的数据文件或外部库。这种方式在加载时干净地失败(甚至可以测试这种失败),而不是以后不可预测。

#3


4  

From wikipedia Perl module:

来自*Perl模块:

   A Perl module must end with a true value or else it is considered not to
   have loaded.  By convention this value is usually 1 though it can be
   any true value.  A module can end with false to indicate failure but
   this is rarely used and it would instead die() (exit with an error).

#4


3  

1;

1;

When a module is loaded (via use) the compiler will complain unless the last statement executed when it is loaded is true. This line ensures that this is the case (as long as you don't place any code after this line). It's Perl's way of making sure that it successfully parsed all the way to the end of the file.

加载模块(通过使用)时,除非加载的最后一个语句为真,否则编译器会报警。这一行确保了这种情况(只要您不在此行之后放置任何代码)。它是Perl确保成功解析到文件末尾的方法。

See http://mathforum.org/~ken/perl_modules.html

见http://mathforum.org/~ken/perl_modules.html

You can use any statement that evaluates as true. 1 just happened to a become a perl idiom.

您可以使用任何计算结果为true的语句。 1恰好碰巧变成了一个perl成语。

#1


36  

Because Perl modules are required to return a value to signal if the require directive must succeed (true value returned) or fail (false value returned; this can make sense if the module failed to initialize for some reason).

因为如果require指令必须成功(返回true值)或失败(返回false值,则需要Perl模块返回一个值来发出信号;如果模块由于某种原因未能初始化,这可能是有意义的)。

If you don't return anything, the interpreter cannot know if the require must succeed or fail; at the same time, since it's easy to forget to put the true value at the end of the package, it suggests the "common fix" for this error: add a true value as a return.

如果您不返回任何内容,则口译员无法知道要求是否必须成功或失败;同时,由于很容易忘记将真值放在包的末尾,它建议对此错误进行“常见修复”:添加一个真值作为返回。

For some other info/folklore about the modules return value have a look at this question.

有关模块返回值的其他信息/民间传说,请查看此问题。

#2


5  

A package can return a false value if it fails to initialize, for example if it couldn't find a required data file or external library. This way it fails cleanly at load time (and this failure can even be tested for) rather than unpredictably later.

如果包无法初始化,则包可以返回false值,例如,如果它找不到所需的数据文件或外部库。这种方式在加载时干净地失败(甚至可以测试这种失败),而不是以后不可预测。

#3


4  

From wikipedia Perl module:

来自*Perl模块:

   A Perl module must end with a true value or else it is considered not to
   have loaded.  By convention this value is usually 1 though it can be
   any true value.  A module can end with false to indicate failure but
   this is rarely used and it would instead die() (exit with an error).

#4


3  

1;

1;

When a module is loaded (via use) the compiler will complain unless the last statement executed when it is loaded is true. This line ensures that this is the case (as long as you don't place any code after this line). It's Perl's way of making sure that it successfully parsed all the way to the end of the file.

加载模块(通过使用)时,除非加载的最后一个语句为真,否则编译器会报警。这一行确保了这种情况(只要您不在此行之后放置任何代码)。它是Perl确保成功解析到文件末尾的方法。

See http://mathforum.org/~ken/perl_modules.html

见http://mathforum.org/~ken/perl_modules.html

You can use any statement that evaluates as true. 1 just happened to a become a perl idiom.

您可以使用任何计算结果为true的语句。 1恰好碰巧变成了一个perl成语。