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


In reply to Re: Short-circuiting a map list. by davido
in thread Short-circuiting a map list. by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.