File Coverage

blib/lib/Iterator/Records/Lines.pm
Criterion Covered Total %
statement 14 49 28.5
branch 0 18 0.0
condition 0 3 0.0
subroutine 5 7 71.4
pod 1 1 100.0
total 20 78 25.6


line stmt bran cond sub pod time code
1             package Iterator::Records::Lines;
2              
3 1     1   56181 use 5.006;
  1         4  
4 1     1   4 use strict;
  1         2  
  1         19  
5 1     1   4 use warnings;
  1         1  
  1         34  
6 1     1   440 use parent 'Iterator::Records';
  1         276  
  1         4  
7 1     1   9529 use Data::Dumper;
  1         5733  
  1         365  
8              
9             =head1 NAME
10              
11             Iterator::Records::Lines - Provides simple record iterators for reading text line by line
12              
13             =head1 VERSION
14              
15             Version 0.01
16              
17             =cut
18              
19             our $VERSION = '0.01';
20              
21              
22             =head1 SYNOPSIS
23              
24             This module specializes L to provide record iterators returning one line at a time from either a file or a string containing text.
25             The iterator can optionally keep track of text indentation. All line record iterators return the columns (type, lno, indent, len, text); the type is either
26             "line" or "blank". if indentation is not being tracked ("raw input") then the type is always "line", the indent is always 0, and trailing space will not be stripped
27             from the text. (The primary use I have for the line reader is as a pre-tokenizer for parsing input.)
28              
29             =head1 new (input_type, input_source, raw)
30              
31             Creates an iterator factory. The C is either 'file' or 'string', and the source is then either the filename or the string. The string can be a reference to a string.
32             By default, the input line is cleaned up and its non-blank
33              
34             =cut
35              
36             sub new {
37 0     0 1   my $class = shift;
38              
39 0           my $self = bless ({}, $class);
40 0           $self->{input_type} = shift;
41            
42 0 0         if (ref ($self->{input_type}) eq 'CODE') {
43             # This is a transmogrification of a line iterator, so keep the class intact but initialize like a vanilla itrecs.
44             # Transmogrification creates a new itrecs of the parent's class because the parent might have custom transmogrifiers. (See thread on 2019-02-23.)
45 0           $self->{gen} = $self->{input_type};
46 0           $self->{f} = shift;
47 0           $self->{id} = '*';
48 0           return $self;
49             }
50            
51             # Else this is an initial line iterator and we know our fields.
52 0           $self->{f} = ['type', 'lno', 'indent', 'len', 'text'];
53 0           $self->{id} = '*';
54 0           my $input = shift;
55 0           $self->{input_source} = $input;
56 0 0 0       $self->{input_source} = \$input if $self->{input_type} eq 'string' and not ref $input;
57 0           my $raw = shift;
58            
59             $self->{gen} = sub {
60 0     0     my $done = 0;
61 0 0         open my $fh, '<', $self->{input_source} or $done = 1;
62 0 0         if ($done) {
63 0           $self->{file_error} = $!;
64             }
65 0           my $lno = 0;
66             sub {
67 0 0         return undef if $done;
68            
69 0           $lno += 1;
70 0           my $line = <$fh>;
71 0 0         if (not $line) {
72 0           $done = 1;
73 0           return undef;
74             }
75 0           $line =~ s/[\r\n]*$//;
76 0 0         return ['line', $lno, 0, length($line), $line] if $raw;
77            
78 0           $line =~ s/(\s+)$//;
79 0 0         return ['blank', $lno, 0, 0, ''] unless length($line);
80            
81 0 0         if ($line =~ /^(\s+)(.*)/) {
82 0           return ['line', $lno, length($1), length($2), $2];
83             }
84            
85 0           return ['line', $lno, 0, length($line), $line];
86             }
87 0           };
  0            
88 0           $self;
89             }
90              
91              
92             =head1 AUTHOR
93              
94             Michael Roberts, C<< >>
95              
96             =head1 BUGS
97              
98             Please report any bugs or feature requests to C, or through
99             the web interface at L. I will be notified, and then you'll
100             automatically be notified of progress on your bug as I make changes.
101              
102              
103              
104              
105             =head1 SUPPORT
106              
107             You can find documentation for this module with the perldoc command.
108              
109             perldoc Iterator::Records::Lines
110              
111              
112             You can also look for information at:
113              
114             =over 4
115              
116             =item * RT: CPAN's request tracker (report bugs here)
117              
118             L
119              
120             =item * AnnoCPAN: Annotated CPAN documentation
121              
122             L
123              
124             =item * CPAN Ratings
125              
126             L
127              
128             =item * Search CPAN
129              
130             L
131              
132             =back
133              
134              
135             =head1 ACKNOWLEDGEMENTS
136              
137              
138             =head1 LICENSE AND COPYRIGHT
139              
140             Copyright 2021 Michael Roberts.
141              
142             This program is free software; you can redistribute it and/or modify it
143             under the terms of the the Artistic License (2.0). You may obtain a
144             copy of the full license at:
145              
146             L
147              
148             Any use, modification, and distribution of the Standard or Modified
149             Versions is governed by this Artistic License. By using, modifying or
150             distributing the Package, you accept this license. Do not use, modify,
151             or distribute the Package, if you do not accept this license.
152              
153             If your Modified Version has been derived from a Modified Version made
154             by someone other than you, you are nevertheless required to ensure that
155             your Modified Version complies with the requirements of this license.
156              
157             This license does not grant you the right to use any trademark, service
158             mark, tradename, or logo of the Copyright Holder.
159              
160             This license includes the non-exclusive, worldwide, free-of-charge
161             patent license to make, have made, use, offer to sell, sell, import and
162             otherwise transfer the Package with respect to any patent claims
163             licensable by the Copyright Holder that are necessarily infringed by the
164             Package. If you institute patent litigation (including a cross-claim or
165             counterclaim) against any party alleging that the Package constitutes
166             direct or contributory patent infringement, then this Artistic License
167             to you shall terminate on the date that such litigation is filed.
168              
169             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
170             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
171             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
172             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
173             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
174             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
175             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
176             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
177              
178              
179             =cut
180              
181             1; # End of Iterator::Records::Lines