powerin has asked for the wisdom of the Perl Monks concerning the following question:
Hi Monks,
I'd like to make a return from subroutine by the code running in a BLOCK.
I noticed how grep is working in the question:
sub working { my @a = (1); grep { print "grep\n"; return 'leave' } @a; print "Still here #1\n"; } working; __DATA__ grep
So, "Still here #1" is never shown.
grep is a Perl function, but I'd like to get such behaviour in a BLOCK of my own subroutine. Below is my example:
sub mygrep (&@) { my $code = shift; my @result; foreach $_ (@_) { push(@result, $_) if &$code; } @result; } sub notworking { my @a = (1); mygrep { print "mygrep\n"; return 'leave' } @a; print "Still here #2\n"; } notworking; __DATA__ mygrep Still here #2
And this is nothing like original grep makes.
So, the question is: How to run a code in BLOCK passed to a subroutine and call return in a caller?
Back to
Seekers of Perl Wisdom