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.003002';
3 2     2   106118 use strict;
  2         9  
  2         47  
4 2     2   9 use warnings;
  2         3  
  2         44  
5              
6             # ABSTRACT: Implements the Perl 6 'gather/take' control structure in Perl 5
7              
8 2     2   8 use Carp 'croak';
  2         3  
  2         129  
9              
10 2         16 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   824 };
  2         1736  
16              
17             my %gatherers;
18              
19             sub gather(&) {
20 12 50   12 0 4575 croak "Useless use of 'gather' in void context" unless defined wantarray;
21 12         23 my ($code) = @_;
22 12         23 my $caller = caller;
23 12         18 local @_;
24 12         15 push @{$gatherers{$caller}}, bless \@_, 'Syntax::Keyword::Gather::MagicArrayRef';
  12         38  
25             die $@
26 12 50 66     17 if !eval{ &$code } && $@ && !UNIVERSAL::isa($@, 'Syntax::Keyword::Gather::Break');
  12   33     22  
27 12 100       39 return @{pop @{$gatherers{$caller}}} if wantarray;
  11         15  
  11         69  
28 1 50       3 return pop @{$gatherers{$caller}} if defined wantarray;
  1         3  
29             }
30              
31             sub gathered() {
32 6     6 0 39 my $caller = caller;
33 6 100       8 croak "Call to gathered not inside a gather" unless @{$gatherers{$caller}};
  6         83  
34 5         12 return $gatherers{$caller}[-1];
35             }
36              
37             sub take(@) {
38 74     74 0 603 my $caller = caller;
39 74 100 100     454 croak "Call to take not inside a gather block"
40             unless ((caller 3)[3]||"") eq 'Syntax::Keyword::Gather::gather';
41 73 100       153 @_ = $_ unless @_;
42 73         80 push @{$gatherers{$caller}[-1]}, @_;
  73         161  
43 73         117 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.003002';
54             use overload
55 5     5   13 'bool' => sub { @{$_[0]} > 0 },
  5         18  
56 0     0   0 '0+' => sub { @{$_[0]} + 0 },
  0         0  
57 1     1   215 '""' => sub { join q{}, @{$_[0]} },
  1         67  
58 2     2   2619 fallback => 1;
  2         1601  
  2         15  
59              
60             1;
61              
62             __END__