Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between unless () return ... ,and return ... unless()

Tags:

perl

In Perl, is there any meaningful difference between:

return $result unless ($exist_condition);

and

unless ($exist_condition) return $result;
like image 699
ado Avatar asked Feb 04 '26 17:02

ado


1 Answers

The second is a syntax error. I presume you meant

# unless statement modifier
return $result unless $exist_condition;

and

# unless statement
unless ($exist_condition) { return $result; }

They're virtually the same. One difference is that the unless statement creates a scope (two actually), while the unless statement modifier does not.

>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

In practice, that will probably never come up, so the difference is merely a question of style.

like image 161
ikegami Avatar answered Feb 07 '26 23:02

ikegami