File Coverage

blib/lib/Iterator/Flex/Grep.pm
Criterion Covered Total %
statement 53 54 98.1
branch 8 12 66.6
condition 1 3 33.3
subroutine 13 13 100.0
pod 1 2 50.0
total 76 84 90.4


line stmt bran cond sub pod time code
1             package Iterator::Flex::Grep;
2              
3             # ABSTRACT: Grep Iterator Class
4              
5 5     5   316884 use v5.28;
  5         21  
6 5     5   35 use strict;
  5         11  
  5         160  
7 5     5   29 use warnings;
  5         10  
  5         369  
8 5     5   622 use experimental 'signatures';
  5         2069  
  5         38  
9              
10             our $VERSION = '0.33';
11              
12 5     5   1765 use Iterator::Flex::Factory 'to_iterator';
  5         13  
  5         471  
13 5     5   35 use Iterator::Flex::Utils qw[ THROW STATE EXHAUSTION :IterAttrs :IterStates ];
  5         74  
  5         1216  
14 5     5   37 use Ref::Util;
  5         11  
  5         292  
15 5     5   32 use parent 'Iterator::Flex::Base';
  5         10  
  5         62  
16              
17 5     5   896 use namespace::clean;
  5         11  
  5         40  
18              
19              
20              
21              
22              
23              
24              
25              
26              
27              
28              
29              
30              
31              
32              
33              
34              
35              
36              
37              
38              
39              
40              
41              
42              
43              
44              
45              
46 7     7 1 13 sub new ( $class, $code, $iterable, $pars = {} ) {
  7         17  
  7         15  
  7         12  
  7         15  
  7         32  
47 7 50       29 throw_failure( parameter => q{'code' parameter is not a coderef} )
48             unless Ref::Util::is_coderef( $code );
49              
50 7         49 $class->SUPER::new( { code => $code, src => $iterable }, $pars );
51             }
52              
53              
54 7     7 0 14 sub construct ( $class, $state ) {
  7         14  
  7         14  
  7         12  
55              
56 7 50       47 throw_failure( parameter => q{'state' parameter must be a HASH reference} )
57             unless Ref::Util::is_hashref( $state );
58              
59 7         13 my ( $code, $src ) = @{$state}{qw[ code src ]};
  7         21  
60              
61 7         51 $src
62             = to_iterator( $src, { ( +EXHAUSTION ) => THROW } );
63              
64 7         24 my $self;
65             my $iterator_state;
66              
67             return {
68             ( +_NAME ) => 'igrep',
69              
70             ( +_SELF ) => \$self,
71              
72             ( +STATE ) => \$iterator_state,
73              
74             ( +NEXT ) => sub {
75 24 50   24   67 return $self->signal_exhaustion
76             if $iterator_state == IterState_EXHAUSTED;
77              
78             my $ret = eval {
79 24         39 while ( 1 ) {
80 35         204 my $rv = $src->();
81 28         69 local $_ = $rv;
82 28 100       84 return $rv if $code->();
83             }
84 0         0 1;
85 24 100       46 } or do {
86 7 50 33     544 die $@
87             unless Ref::Util::is_blessed_ref( $@ )
88             && $@->isa( 'Iterator::Flex::Failure::Exhausted' );
89 7         36 return $self->signal_exhaustion;
90             };
91 17         120 return $ret;
92             },
93       2     ( +RESET ) => sub { },
94 7         153 ( +_DEPENDS ) => $src,
95             };
96             }
97              
98             __PACKAGE__->_add_roles( qw[
99             State::Closure
100             Next::ClosedSelf
101             Rewind::Closure
102             Reset::Closure
103             Current::Closure
104             ] );
105              
106             1;
107              
108             #
109             # This file is part of Iterator-Flex
110             #
111             # This software is Copyright (c) 2018 by Smithsonian Astrophysical Observatory.
112             #
113             # This is free software, licensed under:
114             #
115             # The GNU General Public License, Version 3, June 2007
116             #
117              
118             __END__