File Coverage

blib/lib/Syntax/Keyword/Gather.pm
Criterion Covered Total %
statement 42 45 93.3
branch 11 14 78.5
condition 5 8 62.5
subroutine 10 12 83.3
pod 0 4 0.0
total 68 83 81.9


line stmt bran cond sub pod time code
1             package Syntax::Keyword::Gather;
2             $Syntax::Keyword::Gather::VERSION = '1.003001';
3 2     2   45345 use strict;
  2         4  
  2         75  
4 2     2   10 use warnings;
  2         3  
  2         60  
5              
6             # ABSTRACT: Implements the Perl 6 'gather/take' control structure in Perl 5
7              
8 2     2   11 use Carp 'croak';
  2         7  
  2         260  
9              
10 2         24 use Sub::Exporter::Progressive -setup => {
11             exports => [qw{ break gather gathered take }],
12             groups => {
13             default => [qw{ break gather gathered take }],
14             },
15 2     2   1753 };
  2         2386  
16              
17             my %gatherers;
18              
19             sub gather(&) {
20 12 50   12 0 5353 croak "Useless use of 'gather' in void context" unless defined wantarray;
21 12         23 my ($code) = @_;
22 12         24 my $caller = caller;
23 12         23 local @_;
24 12         15 push @{$gatherers{$caller}}, bless \@_, 'Syntax::Keyword::Gather::MagicArrayRef';
  12         56  
25             die $@
26 12 50 66     19 if !eval{ &$code } && $@ && !UNIVERSAL::isa($@, 'Syntax::Keyword::Gather::Break');
  12   33     30  
27 12 100       54 return @{pop @{$gatherers{$caller}}} if wantarray;
  11         13  
  11         88  
28 1 50       5 return pop @{$gatherers{$caller}} if defined wantarray;
  1         7  
29             }
30              
31             sub gathered() {
32 6     6 0 27 my $caller = caller;
33 6 100       10 croak "Call to gathered not inside a gather" unless @{$gatherers{$caller}};
  6         94  
34 5         18 return $gatherers{$caller}[-1];
35             }
36              
37             sub take(@) {
38 74     74 0 659 my $caller = caller;
39 74 100 100     628 croak "Call to take not inside a gather block"
40             unless ((caller 3)[3]||"") eq 'Syntax::Keyword::Gather::gather';
41 73 100       227 @_ = $_ unless @_;
42 73         77 push @{$gatherers{$caller}[-1]}, @_;
  73         227  
43 73         200 return 0+@_;
44             }
45              
46             my $breaker = bless [], 'Syntax::Keyword::Gather::Break';
47              
48             sub break() {
49 0     0 0   die $breaker;
50             }
51              
52             package Syntax::Keyword::Gather::MagicArrayRef;
53             $Syntax::Keyword::Gather::MagicArrayRef::VERSION = '1.003001';
54             use overload
55 5     5   17 'bool' => sub { @{$_[0]} > 0 },
  5         21  
56 0     0   0 '0+' => sub { @{$_[0]} + 0 },
  0         0  
57 1     1   150 '""' => sub { join q{}, @{$_[0]} },
  1         35  
58 2     2   4564 fallback => 1;
  2         2255  
  2         29  
59              
60             1;
61              
62             __END__