为什么这个Perl代码使用1;陈述
可能重复:为什么这个Perl代码使用1;陈述,perl,Perl,可能重复: 从这个页面,这是一个代码示例 package Password; # every time the user guesses the password wrong, its value # is rotated by one character my $password; sub set_password { $password = shift; } sub check_password { my $guess = shift; if ($guess eq
从这个页面,这是一个代码示例
package Password;
# every time the user guesses the password wrong, its value
# is rotated by one character
my $password;
sub set_password {
$password = shift;
}
sub check_password {
my $guess = shift;
if ($guess eq $password) {
unlock_secrets();
} else {
$password = (substr $password, 1).(substr $password, 0, 1);
}
}
1;
- 为什么a
1代码>在末尾使用?该语句对编译器意味着什么
我试过代码,然后就成功了。结果看起来是一样的。这是因为Perl模块必须返回“true”才能作为模块工作。如果模块没有返回true,则假定其初始化失败,这相当于调用
die
。但是,如果代码没有用作模块,则不需要它,因此除非您尝试将代码示例用作模块,否则您不会看到区别
1
可能只是返回true的最简洁的方法之一
如果你看一看,你会发现人们对他们的回报值非常有创意。以下是一段引语:
文件必须作为最后一个返回true
表示成功的声明
执行任何初始化代码,
因此,通常结束这样一个文件
1例;除非你确定它会
否则返回true。但这样更好
只想说一句,如果你加上
更多声明
如果我删除1代码>从您的模块,然后尝试在另一个脚本中执行,我得到一个编译错误:
Password.pm did not return a true value
BEGIN failed--compilation aborted
与okay相关,基本上除了0
,'0'
,undef
,,(
,),(“”)
@Lazer:甚至'false'
都是真的。