in reply to Short-circuiting a map list.

This doesn't give you "return()", but it does give you the following short-circuiting alternatives, two of which can still return a list:

bnext # terminates current iteration without any 'push', but a list # will still be returned if other iterations resulted # in a push. blast # terminates loop. Any 'push'es from previous # iterations are retained in results. bbail # Bails out with no result set (returns () )

The need for multiple return points can be mostly resolved with next and last, and of course with good old structured programming techniques. Here's the module implementing bmap{}(); a map that allows last, next, and bail:

package Bmap; use strict; use warnings; use Exporter qw/ import /; our @EXPORT = qw/bmap bnext blast bbail/; sub bnext() { no warnings 'exiting'; next; } sub blast() { no warnings 'exiting'; last; } sub bbail() { goto BBAIL; } sub bmap(&@) { my $f = shift; my @return_value; for ( @_ ) { my @iteration_results = $f->(); push @return_value, @iteration_results; } return @return_value; BBAIL: return (); } 1;

And now here is some code to test it. Notice the first example passes over 'and' in the list. The second example passes over 'and', and everything that came after it. And the third example bails out with ().

use strict; use warnings; use v5.12; use Bmap; my @array = qw/ this that and the other /; my @nexted = bmap{ $_ eq 'and' && bnext(); $_ } @array; my @lasted = bmap{ $_ eq 'and' && blast(); $_ } @array; my @bailed = bmap{ $_ eq 'and' && bbail(); $_ } @array; say "'next' version: @nexted"; say "'last' version: @lasted"; say "'bail' version: @bailed";

And the test run:

'next' version: this that the other 'last' version: this that 'bail' version:

The hard part was coming up with a solution that would squelch the "exiting" warning. At first my implementation didn't create its own "next" and "last" functions, and that meant there was nowhere I could put the no warnings that would actually propagate out to the necessary scope without cluttering the bmap's usage.

...no good reason for the function's name...

This may fall short somehow. But I'm interested in your thoughts on this approach.

First (and maybe last) time I've used the unpopular version of goto non-jokingly. *shudder*


Dave