除非()返回...之间的区别,并返回...除非()

ado*_*ado 2 perl

在Perl中,之间是否有任何有意义的区别:

return $result unless ($exist_condition);
Run Code Online (Sandbox Code Playgroud)

unless ($exist_condition) return $result;
Run Code Online (Sandbox Code Playgroud)

ike*_*ami 8

第二个是语法错误.我认为你的意思

# unless statement modifier
return $result unless $exist_condition;
Run Code Online (Sandbox Code Playgroud)

# unless statement
unless ($exist_condition) { return $result; }
Run Code Online (Sandbox Code Playgroud)

他们几乎是一样的.一个区别是unless语句创建了一个范围(实际上是两个),而unless语句修饰符则没有.

>perl -E"my $x = 'abc'; unless (my $x = 'xyz') { return; } say $x;"
abc

>perl -E"my $x = 'abc'; return unless my $x = 'xyz'; say $x;"
xyz
Run Code Online (Sandbox Code Playgroud)

在实践中,这可能永远不会出现,因此差异仅仅是风格问题.