File Coverage

blib/lib/Text/PRN/Slurp.pm
Criterion Covered Total %
statement 15 67 22.3
branch 0 20 0.0
condition 0 8 0.0
subroutine 5 8 62.5
pod 2 2 100.0
total 22 105 20.9


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