| 1 | package Iterator::Records::Lines; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use parent 'Iterator::Records'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 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 | 1 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | This module specializes L<Iterator::Records> 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<input_type> 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 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 37 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 0 | my $self = bless ({}, $class); | |||||||||||||||||||||||||||
| 40 | 0 | $self->{input_type} = shift; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 0 | ▶ | if (ref ($self->{input_type}) eq 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | ▶ | $self->{input_source} = \$input if $self->{input_type} eq 'string' and not ref $input; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"input_type"} eq "string" and not ref $input
MC/DC0%0/2
$self->{"input_type"} eq "string" and not ref $input
$self->{"input_type"} eq "string"
not ref $input
Truth table0%0/3
A and not(B)
A= $self->{"input_type"} eq "string"B= not ref $input
| |||||||||||||||||||||||||||||
| 57 | 0 | my $raw = shift; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 0 | ▶ | $self->{gen} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 60 | 0 | my $done = 0; | |||||||||||||||||||||||||||
| 61 | 0 | ▶ | open my $fh, '<', $self->{input_source} or $done = 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 0 | ▶ | if ($done) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 0 | $self->{file_error} = $!; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | 0 | my $lno = 0; | |||||||||||||||||||||||||||
| 66 | 0 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 67 | 0 | ▶ | return undef if $done; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | $lno += 1; | |||||||||||||||||||||||||||
| 70 | 0 | my $line = <$fh>; | |||||||||||||||||||||||||||
| 71 | 0 | ▶ | if (not $line) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 0 | $done = 1; | |||||||||||||||||||||||||||
| 73 | 0 | return undef; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | 0 | $line =~ s/[\r\n]*$//; | |||||||||||||||||||||||||||
| 76 | 0 | ▶ | return ['line', $lno, 0, length($line), $line] if $raw; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 0 | $line =~ s/(\s+)$//; | |||||||||||||||||||||||||||
| 79 | 0 | ▶ | return ['blank', $lno, 0, 0, ''] unless length($line); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | ▶ | if ($line =~ /^(\s+)(.*)/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 0 | return ['line', $lno, length($1), length($2), $2]; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 0 | return ['line', $lno, 0, length($line), $line]; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | 0 | }; | |||||||||||||||||||||||||||
| 88 | 0 | $self; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | Michael Roberts, C<< <michael at vivtek.com> >> | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | =head1 BUGS | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | Please report any bugs or feature requests to C<bug-iterator-records-lines at rt.cpan.org>, or through | ||||||||||||||||||||||||||||
| 99 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Iterator-Records-Lines>. 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<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Iterator-Records-Lines> | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | L<http://annocpan.org/dist/Iterator-Records-Lines> | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | =item * CPAN Ratings | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | L<http://cpanratings.perl.org/d/Iterator-Records-Lines> | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | =item * Search CPAN | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | L<http://search.cpan.org/dist/Iterator-Records-Lines/> | ||||||||||||||||||||||||||||
| 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<http://www.perlfoundation.org/artistic_license_2_0> | ||||||||||||||||||||||||||||
| 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 | 1; # End of Iterator::Records::Lines | |||||||||||||||||||||||||||