这是从perl中的子例程返回错误消息的好方法吗?
sub some_subroutine{
# do something
$something = 14;
if(1 == 2){
$_ = "This should not be happening!";
return undef;
}
return $something;
}
my $ret=some_subroutine();
print "ERROR: $_" unless(defined $ret);
Run Code Online (Sandbox Code Playgroud)
代码运行正常(在并行世界中,在哪里1 == 2),但使用$_返回错误消息是一个好方法?我没有找到任何关于$_此类用途的使用文档.
谢谢!
我想在perl中编写一些设置$的函数!以类似于内置perl函数的方式.当我尝试这样做时,它会抱怨"参数"无法创建管理员用户"在标量赋值中不是数字".我试过谷歌搜索这个但不幸的是谷歌不会看$!所以结果很难得到.
if(!createUser('admin')) { print $!; }
sub createUser
{
my ($user) = @_;
if($user eq "admin")
{
$! = "Cannot create admin user";
return 0;
}
#create user here
return 1;
}
Run Code Online (Sandbox Code Playgroud)
PS.我知道Try :: Tiny等,但我想保持与脚本其余部分的一致性,如果我切换到try/catch我想移动整个脚本,我还没准备好.
perl ×2