note
Eily
<p>I must admit that I'm a little surprised myself, but it turns out that [doc://or]'s [doc://perlop|precedence] is so low that it is actually lower than [doc://return] (probably because [doc://return] is treated as a list operator). Which leads to the following result:
<c>perl -MO=Deparse -e "sub test { return int(rand(2)) or print 'Nope' }"
sub test {
print 'Nope' unless return int rand 2;
}
-e syntax OK
</c>
I'm pretty sure this is not what you meant, because this would somehow try to do something in the sub <i>after</i> returning from it. I'm also not exactly sure what the return value of [doc://return] itself would be :P.</p>
<p>Two ways to do what you want (beyond the one you already proposed), either with a temp variable:
<c>my $result;
$result = something() or croak "Something went wrong";
return $result; </c>
Or, if this is the last line of your sub, you could remove the [doc://return] altogether (if that's compatible with your coding rules):
<c>sub mySub
{
...
something() or croak "Times are a'croaking";
}</c>
The <i>right</i> way is the one that you like the most (again, respecting whatever coding rules should apply).</p>
11111352
11111352