List::Gather - Construct lists procedurally without temporary variables
version 0.14
use List::Gather; my @list = gather { while (<$fh>) { next if /^\s*$/; next if /^\s*#/; last if /^(?:__END__|__DATA__)$/; take $_ if some_predicate($_); } take @defaults unless gathered; };
This module provides a gather keyword that allows lists to be constructed procedurally, without the need for a temporary variable.
gather
Within the block controlled by a gather any call to take pushes that call's argument list to an implicitly created array.
take
gather returns the list of values taken during its block's execution.
gather { ... } gather({ ... }) gather STMT
Executes the block it has been provided with, collecting all arguments passed to take calls within it. After execution, the list of values collected is returned.
Note that block gather executes is equivalent to a do BLOCK. It is neither a code nor a loop. Loop control keywords, such as next and last, as well as return will behave accordingly.
do BLOCK
next
last
return
Parens around the gather block are optional.
take LIST
Collects a LIST of values within the gather block it has been compiled in.
LIST
take returns all its arguments.
take calls outside of the lexical scope of a gather block are compile time errors. Calling take is only legal within the dynamic scope its associated gather block.
gathered
Returns the list of items collected so far during the execution of a gather block.
gathered calls outside of the lexical scope of a gather block are compile time errors. Calling gathered outside of the dynamic scope of its associated gather block is legal.
my @interesting_child_nodes = gather for my $n (@nodes) { take $n->all_children if $n->is_interesting; }; my @last_10_events = gather { while ($log->has_event) { take $log->next_event; } shift gathered while gathered > 10; }; my @search_results = gather { $user_interface->register_status_callback(sub { sprintf "Searching... Found %d matches so far", scalar gathered; }); wait_for_search_results(sub { my ($result) = @_; take $result; }, @search_terms); $user_interface->register_status_callback(sub { sprintf "Found a total of %d", scalar gathered; }); }; my @leaf_nodes = gather { $graph->visit_all_nodes_recursively(sub { my ($node) = @_; take $node if $node->is_leaf; }); };
A non-lexical gather/take implementation that's otherwise very similar to this one
An experimental implementation of a lazily evaluating gather/take
A very simple gather/take implementation without lexical scoping
Like Syntax::Keyword::Gather, but reliant on Perl6::Export
A comprehensive suit list generation functions featuring a non-lexical gather/take
Andrew Main (Zefram) <zefram@fysh.org>
for providing his input in both the design and implementation of this module, and writing much of the infrastructure that made this module possible in the first place
Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>
for his input on various aspects of this module as well as the many tests of his Syntax::Keyword::Gather module that this module shamelessly stole
Dave (autarch) Rolsky <autarch@urth.org> and Jesse (doy) Luehrs <doy@tozt.net>
for helping to improve both documentation and test coverage
Bugs may be submitted through the RT bug tracker (or bug-List-Gather@rt.cpan.org).
Florian Ragwitz <rafl@debian.org>
Karen Etheridge <ether@cpan.org>
Father Chrysostomos <sprout@cpan.org>
David Mitchell <davem@iabyn.com>
Tony Cook <tonyc@cpan.org>
bay-max1 <34803732+bay-max1@users.noreply.github.com>
This software is copyright (c) 2012 by Florian Ragwitz.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install List::Gather, copy and paste the appropriate command in to your terminal.
cpanm
cpanm List::Gather
CPAN shell
perl -MCPAN -e shell install List::Gather
For more information on module installation, please visit the detailed CPAN module installation guide.