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?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Bare BLOCK vrs. grep BLOCK
by AnomalousMonk (Archbishop) on Jan 16, 2018 at 01:03 UTC | |
Re: Bare BLOCK vrs. grep BLOCK
by kcott (Archbishop) on Jan 16, 2018 at 10:26 UTC | |
Re: Bare BLOCK vrs. grep BLOCK
by AnomalousMonk (Archbishop) on Jan 16, 2018 at 02:32 UTC | |
Re: Bare BLOCK vrs. grep BLOCK
by vr (Curate) on Jan 16, 2018 at 17:52 UTC | |
Re: Bare BLOCK vrs. grep BLOCK
by ikegami (Patriarch) on Jan 17, 2018 at 04:56 UTC | |
Re: Bare BLOCK vrs. grep BLOCK
by pritesh (Scribe) on Jan 15, 2018 at 20:58 UTC | |
by mr_mischief (Monsignor) on Jan 15, 2018 at 21:34 UTC |
Back to
Seekers of Perl Wisdom