Keep It Simple, Stupid | |
PerlMonks |
Re: Bare BLOCK vrs. grep BLOCKby vr (Curate) |
on Jan 16, 2018 at 17:52 UTC ( [id://1207363]=note: print w/replies, xml ) | Need Help?? |
As pointed in other answers, the BLOCK as argument to grep is Perl's black magic, but BLOCK as argument to a subroutine prototyped with (&) is just syntactic sugar to allow to omit the sub keyword. So the latter is not "bare BLOCK", but subroutine body, and the return statement returns one frame above, as it should. More specifically: An & requires an anonymous subroutine, which, if passed as the first argument, does not require the sub keyword or a subsequent comma. And: return In effect, it's same question, as "Is it possible for a Perl subroutine to force its caller to return?". Following the CPAN links, here's a solution using the Scope::Upper, with a bit convoluted example. Nevertheless, I think it answers your question, i.e. the maybe_working sub doesn't have to take any additional measures for this example to work. The anon sub ALWAYS returns normally, so its the my_grep responsibility to check the return value (so there's reserved return value) and decide, whether to continue or return to 2 stack frames above.
Output:
In Section
Seekers of Perl Wisdom
|
|