File Coverage

blib/lib/Data/Grid/Table.pm
Criterion Covered Total %
statement 15 30 50.0
branch 0 2 0.0
condition n/a
subroutine 5 12 41.6
pod 7 7 100.0
total 27 51 52.9


line stmt bran cond sub pod time code
1             package Data::Grid::Table;
2              
3 1     1   7 use warnings FATAL => 'all';
  1         1  
  1         31  
4 1     1   4 use strict;
  1         2  
  1         17  
5              
6 1     1   4 use base 'Data::Grid::Container';
  1         1  
  1         358  
7              
8 1     1   5 use overload '<>' => "next";
  1         1  
  1         3  
9 1     1   50 use overload '@{}' => "rows";
  1         1  
  1         3  
10              
11             =head1 NAME
12              
13             Data::Grid::Table - A table implementation for Data::Grid
14              
15             =head1 VERSION
16              
17             Version 0.01_01
18              
19             =cut
20              
21             our $VERSION = '0.01_01';
22              
23              
24             =head1 SYNOPSIS
25              
26             my $grid = Data::Grid->parse('arbitrary.csv');
27              
28             # Just take the first one, since a CSV will only have one table.
29             my ($table) = $grid->tables;
30              
31             while (my $row = $table->next) {
32             # do some stuff
33             }
34              
35             # or
36              
37             while (my $row = <$table>) {
38             # ...
39             }
40              
41             # or
42              
43             my @rows = $table->rows;
44              
45             # or
46              
47             my @rows = @$table;
48              
49             =head1 METHODS
50              
51             =head2 next
52              
53             Retrieves the next row in the table or C when it reaches the
54             end. This method L by a driver subclass. The
55             iteration operator C<<>> is also overloaded for table objects, so you
56             can use it like this:
57              
58             while (my $row = <$table>) { ...
59              
60             =cut
61              
62             sub next {
63 0     0 1   Carp::croak("This method is a stub; it must be overridden!");
64             }
65              
66             =head2 first
67              
68             Returns the first row in the table, and is equivalent to calling
69             L and then L.
70              
71             =cut
72              
73             sub first {
74 0     0 1   my $self = shift;
75 0           $self->rewind;
76 0           $self->next;
77             }
78              
79             =head2 rewind
80              
81             Sets the table's cursor back to the first row. Returns the previous
82             position, beginning at zero. This method I by a
83             driver subclass.
84              
85             =cut
86              
87             sub rewind {
88 0     0 1   Carp::croak("This method is a stub; it must be overridden!");
89             }
90              
91             =head2 rows
92              
93             Retrieves an array of rows all at once, or if taken in scalar context,
94             an array reference. This method overloads the array dereferencing
95             operator C<@{}>, so you can use it like this:
96              
97             my @rows = @$table;
98              
99             =cut
100              
101             sub rows {
102 0     0 1   my ($self, @rows) = @_;
103             #my @rows;
104              
105 0           $self->rewind;
106 0           while (my $row = $self->next) {
107 0           push @rows, $row;
108             }
109 0           $self->rewind;
110              
111 0 0         wantarray ? @rows : \@rows;
112             }
113              
114             =head2 columns
115              
116             =cut
117              
118             sub columns {
119 0     0 1   $_[0]->parent->columns;
120             }
121              
122             =head2 width
123              
124             Gets the width, in columns, of the table.
125              
126             =cut
127              
128             =head2 height
129              
130             Gets the height, in rows, of the table. Careful, for drivers that only
131             do sequential access, this means iterating over the whole set, so you
132             might as well.
133              
134             =cut
135              
136             sub height {
137 0     0 1   scalar @{$_[0]->rows};
  0            
138             }
139              
140             =head2 as_string
141              
142             Serializes the table into a string and returns the result. Currently
143             unimplemented.
144              
145             =cut
146              
147             #sub as_string {
148             #
149             #}
150              
151             =head2 as_data_table
152              
153             Returns a new Data::Table object for compatibility.
154              
155             =cut
156              
157             sub as_data_table {
158 0     0 1   require Data::Table;
159             }
160              
161             =head1 AUTHOR
162              
163             Dorian Taylor, C<< >>
164              
165             =head1 BUGS
166              
167             Please report any bugs or feature requests to C
168             rt.cpan.org>, or through the web interface at
169             L. I will
170             be notified, and then you'll automatically be notified of progress on
171             your bug as I make changes.
172              
173             =head1 SUPPORT
174              
175             You can find documentation for this module with the perldoc command.
176              
177             perldoc Data::Grid::Table
178              
179             You can also look for information at:
180              
181             =over 4
182              
183             =item * RT: CPAN's request tracker
184              
185             L
186              
187             =item * AnnoCPAN: Annotated CPAN documentation
188              
189             L
190              
191             =item * CPAN Ratings
192              
193             L
194              
195             =item * Search CPAN
196              
197             L
198              
199             =back
200              
201             =head1 SEE ALSO
202              
203             L, L, L,
204             L
205              
206             =head1 LICENSE AND COPYRIGHT
207              
208             Copyright 2010 Dorian Taylor.
209              
210             This program is free software; you can redistribute it and/or modify it
211             under the terms of either: the GNU General Public License as published
212             by the Free Software Foundation; or the Artistic License.
213              
214             See http://dev.perl.org/licenses/ for more information.
215              
216             =cut
217              
218             1; # End of Data::Grid::Table