File Coverage

blib/lib/Text/PRN/Slurp.pm
Criterion Covered Total %
statement 64 71 90.1
branch 14 22 63.6
condition 4 8 50.0
subroutine 9 9 100.0
pod 2 2 100.0
total 93 112 83.0


line stmt bran cond sub pod time code
1             package Text::PRN::Slurp;
2              
3 2     2   40906 use strict;
  2         5  
  2         54  
4 2     2   8 use warnings;
  2         3  
  2         47  
5              
6 2     2   943 use IO::File;
  2         15848  
  2         230  
7 2     2   1053 use IO::Scalar;
  2         11029  
  2         107  
8              
9             =head1 NAME
10              
11             Text::PRN::Slurp - Parse and read .PRN File Extension
12              
13             =head1 VERSION
14              
15             Version 1.04
16              
17             =cut
18              
19 2     2   11 use vars qw/$VERSION/;
  2         3  
  2         1096  
20              
21             $VERSION = 1.04;
22              
23              
24             =head1 SYNOPSIS
25              
26             PRN, short name for Printable, is used as the file extension for files padded with space characters.
27              
28             use Text::PRN::Slurp;
29              
30             my $slurp = Text::PRN::Slurp->new->load(
31             'file' => $file,
32             'file_headers' => [ q{A}, q{B}, q{C}, q{D} ]
33             );
34              
35             =head1 USAGE
36              
37             use Text::PRN::Slurp;
38              
39             my $data = Text::PRN::Slurp->load(file => $filename ,file_headers => ['A','B','C'] [,%options]);
40             my $data = Text::PRN::Slurp->load(filehandle => $filehandle ,file_headers => ['A','B','C'] [,%options]);
41             my $data = Text::PRN::Slurp->load(string => $string ,file_headers => ['A','B','C'] [,%options]);
42              
43             =head1 EXPORT
44              
45             =head2 new
46              
47             Constructors method
48              
49             =cut
50              
51             sub new {
52 3     3 1 88189 my ( $class ) = @_;
53 3         17 return bless {}, $class;
54             }
55              
56             =head2 load
57              
58             my $data = Text::PRN::Slurp->load(file => $filename ,file_headers => ['A','B','C']);
59             my $data = Text::PRN::Slurp->load(filehandle => $filehandle ,file_headers => ['A','B','C']);
60              
61             Returns an arrayref of hashrefs. Its fields are used as the keys for each of the hashes.
62              
63             =cut
64              
65             sub load {
66 2     2 1 8 my ( $self, %opt ) = @_;
67              
68 2         6 my %default = ( binary => 1 );
69 2         10 %opt = (%default, %opt);
70              
71 2 50       10 if ( !defined $opt{'file_headers'} ) {
72 0         0 die "File headers is needed to parse file";
73             }
74 2 50       8 if ( ref $opt{'file_headers'} ne 'ARRAY' ) {
75 0         0 die "File headers needed to be an array";
76             }
77              
78 2 0 33     11 if ( !defined $opt{'filehandle'} &&
      33        
79             !defined $opt{'file'} &&
80             !defined $opt{'string'}
81             ) {
82 0         0 die "Need either a file, filehandle or string to work with";
83             }
84              
85 2         3 my $io;
86 2 50       4 if ( defined $opt{'filehandle'} ) {
87 0         0 $io = $opt{'filehandle'};
88 0         0 delete $opt{'filehandle'};
89             }
90              
91 2 50       18 if ( defined $opt{'file'} ) {
92 2         13 $io = new IO::File;
93 2 50   1   146 open( $io, '<:encoding(UTF-8)', $opt{'file'} )
  1         7  
  1         2  
  1         7  
94             or die "Could not open $opt{file} $!";
95 2         9874 delete $opt{'file'};
96             }
97              
98 2 50       7 if ( defined $opt{'string'} ) {
99 0         0 $io = IO::Scalar->new( \$opt{'string'} );
100 0         0 delete $opt{'string'};
101             }
102              
103 2         7 return _from_io_handler($io,\%opt);
104             }
105              
106             sub _from_io_handler {
107 2     2   4 my ( $io, $opt_ref ) = @_;
108              
109 2         1 my @file_header = @{ $opt_ref->{'file_headers'} };
  2         7  
110 2         3 my ( %col_length_map, @file_data_as_array);
111              
112 2         2 my $row_count = 1;
113 2         42 while (my $row = <$io>) {
114 12         62 chomp $row;
115             ## Assume first row is heading
116 12 100       21 if ( $row_count == 1 ) {
117 2         4 foreach my $col_heading ( @file_header ) {
118 7         75 $row =~m{($col_heading\s+)}i;
119 7 100       23 $row =~m{($col_heading\s?)}i if not $1;
120 7 100       60 if ( $1 ) {
121 6         7 my $table_column = $1;
122 6         10 my $table_column_length = length $table_column;
123             # remove leading and trailing spaces
124 6         25 $table_column =~s{^\s+|\s+$}{}g;
125 6         21 $col_length_map{ $table_column } = $table_column_length;
126             }
127             else {
128 1         19 warn q{Columns doesn't seems to be matching};
129             }
130             }
131             }
132             else {
133 10         8 my $string_offset = 0;
134 10         11 my %extracted_row_data;
135 10         23 for( my $col_index=0; $col_index<=$#file_header; $col_index++ ) {
136 35         36 my $col = $file_header[$col_index];
137 35   100     64 my $col_length = $col_length_map{ $col } || 0;
138 35 100       57 if ( $col_length ) {
139 30         60 my $col_data = substr $row, $string_offset, $col_length;
140             # remove leading and trailing spaces
141 30         109 $col_data =~s{^\s+|\s+$}{}g;
142 30         56 $extracted_row_data{ $col } = $col_data;
143 30         79 $string_offset += $col_length;
144             }
145             }
146              
147 10         17 push @file_data_as_array, \%extracted_row_data;
148             }
149 12         118 $row_count++;
150             }
151              
152 2         37 return \@file_data_as_array;
153             }
154              
155             =head1 AUTHOR
156              
157             Rakesh Kumar Shardiwal, C<< >>
158              
159             =head1 BUGS
160              
161             Please report any bugs or feature requests to C, or through
162             the web interface at L. I will be notified, and then you'll
163             automatically be notified of progress on your bug as I make changes.
164              
165             =head1 SUPPORT
166              
167             You can find documentation for this module with the perldoc command.
168              
169             perldoc Text::PRN::Slurp
170              
171              
172             You can also look for information at:
173              
174             =over 4
175              
176             =item * RT: CPAN's request tracker (report bugs here)
177              
178             L
179              
180             =item * AnnoCPAN: Annotated CPAN documentation
181              
182             L
183              
184             =item * CPAN Ratings
185              
186             L
187              
188             =item * Search CPAN
189              
190             L
191              
192             =back
193              
194              
195             =head1 ACKNOWLEDGEMENTS
196              
197              
198             =head1 LICENSE AND COPYRIGHT
199              
200             Copyright 2016 Rakesh Kumar Shardiwal.
201              
202             This program is free software; you can redistribute it and/or modify it
203             under the terms of the the Artistic License (2.0). You may obtain a
204             copy of the full license at:
205              
206             L
207              
208             Any use, modification, and distribution of the Standard or Modified
209             Versions is governed by this Artistic License. By using, modifying or
210             distributing the Package, you accept this license. Do not use, modify,
211             or distribute the Package, if you do not accept this license.
212              
213             If your Modified Version has been derived from a Modified Version made
214             by someone other than you, you are nevertheless required to ensure that
215             your Modified Version complies with the requirements of this license.
216              
217             This license does not grant you the right to use any trademark, service
218             mark, tradename, or logo of the Copyright Holder.
219              
220             This license includes the non-exclusive, worldwide, free-of-charge
221             patent license to make, have made, use, offer to sell, sell, import and
222             otherwise transfer the Package with respect to any patent claims
223             licensable by the Copyright Holder that are necessarily infringed by the
224             Package. If you institute patent litigation (including a cross-claim or
225             counterclaim) against any party alleging that the Package constitutes
226             direct or contributory patent infringement, then this Artistic License
227             to you shall terminate on the date that such litigation is filed.
228              
229             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
230             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
231             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
232             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
233             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
234             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
235             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
236             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
237              
238              
239             =cut
240              
241             1; # End of Text::PRN::Slurp