File Coverage

blib/lib/AnyData2/Format/Fixed.pm
Criterion Covered Total %
statement 36 38 94.7
branch 2 2 100.0
condition n/a
subroutine 10 11 90.9
pod 4 4 100.0
total 52 55 94.5


line stmt bran cond sub pod time code
1             package AnyData2::Format::Fixed;
2              
3 1     1   812 use 5.008001;
  1         2  
4 1     1   4 use strict;
  1         1  
  1         18  
5 1     1   2 use warnings FATAL => 'all';
  1         1  
  1         26  
6              
7 1     1   2 use base qw(AnyData2::Format AnyData2::Role::GuessImplementation);
  1         1  
  1         303  
8              
9 1     1   3 use Carp qw/croak/;
  1         1  
  1         39  
10 1     1   3 use List::Util '1.29', qw(pairkeys pairvalues);
  1         1  
  1         133  
11 1     1   4 use Module::Runtime qw(require_module);
  1         1  
  1         3  
12              
13             =head1 NAME
14              
15             AnyData2::Format::Fixed - fixed length format class for AnyData2
16              
17             =cut
18              
19             our $VERSION = '0.002';
20              
21             =head1 METHODS
22              
23             =head2 new
24              
25             # pure invocation
26             my $af = AnyData2::Format::Fixed->new(
27             $storage,
28             cols => [ # important: hash changes order!
29             "first" => 20,
30             "second" => 15,
31             ...
32             ]
33             );
34            
35             my $af = AnyData2->new(
36             Fixed => {
37             cols => [ Id => 3, Name => 10, Color => 7, Newline => 1 ]
38             },
39             # a File::Linewise example should do, either
40             "File::Blockwise" => {
41             filename => File::Spec->catfile( $test_dir, "simple.blocks" ),
42             blocksize => 3 + 10 + 7 + 1,
43             filemode => "<:raw"
44             }
45             );
46              
47             constructs a storage, passes all options down to C
48             beside C, which is used to instantiate the parser.
49             C prefers L by default.
50              
51             =cut
52              
53             sub new
54             {
55 1     1 1 2 my ( $class, $storage, %options ) = @_;
56 1         5 my $self = $class->SUPER::new($storage);
57              
58 1         2 $self->{cols} = [ @{ delete $options{cols} } ];
  1         7  
59              
60 1         2 $self;
61             }
62              
63             =head2 cols
64              
65             Deliver the keys of the specification array
66              
67             =cut
68              
69             sub cols
70             {
71 1     1 1 5 my $self = shift;
72 1         1 [ pairkeys @{ $self->{cols} } ];
  1         9  
73             }
74              
75             =head2 fetchrow
76              
77             Extract the values from storages based on the values of the specification array
78              
79             =cut
80              
81             sub fetchrow
82             {
83 4     4 1 11 my $self = shift;
84 4         7 my $buf = $self->{storage}->read();
85 4 100       6 defined $buf or return;
86 3         4 my @data;
87 3         3 foreach my $len ( pairvalues @{ $self->{cols} } )
  3         6  
88             {
89 12         17 push @data, substr $buf, 0, $len, "";
90             }
91 3         6 \@data;
92             }
93              
94             =head2 pushrow
95              
96             Construct buffer based on the values of the specification array and write it into storage (unimplemented)
97              
98             =cut
99              
100             sub pushrow
101             {
102 0     0 1   my ( $self, $fields ) = @_;
103 0           croak "Write support unimplemented. Patches welcome!";
104             }
105              
106             =head1 LICENSE AND COPYRIGHT
107              
108             Copyright 2015,2016 Jens Rehsack.
109              
110             This program is free software; you can redistribute it and/or modify it
111             under the terms of either: the GNU General Public License as published
112             by the Free Software Foundation; or the Artistic License.
113              
114             See http://dev.perl.org/licenses/ for more information.
115              
116             If your Modified Version has been derived from a Modified Version made
117             by someone other than you, you are nevertheless required to ensure that
118             your Modified Version complies with the requirements of this license.
119              
120             This license does not grant you the right to use any trademark, service
121             mark, tradename, or logo of the Copyright Holder.
122              
123             This license includes the non-exclusive, worldwide, free-of-charge
124             patent license to make, have made, use, offer to sell, sell, import and
125             otherwise transfer the Package with respect to any patent claims
126             licensable by the Copyright Holder that are necessarily infringed by the
127             Package. If you institute patent litigation (including a cross-claim or
128             counterclaim) against any party alleging that the Package constitutes
129             direct or contributory patent infringement, then this License
130             to you shall terminate on the date that such litigation is filed.
131              
132             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
133             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
134             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
135             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
136             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
137             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
138             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
139             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
140              
141             =cut
142              
143             1;