File Coverage

blib/lib/Data/Grid/Row.pm
Criterion Covered Total %
statement 18 27 66.6
branch 0 4 0.0
condition n/a
subroutine 6 9 66.6
pod 3 3 100.0
total 27 43 62.7


line stmt bran cond sub pod time code
1             package Data::Grid::Row;
2              
3 1     1   7 use warnings FATAL => 'all';
  1         1  
  1         28  
4 1     1   5 use strict;
  1         1  
  1         17  
5              
6 1     1   4 use base 'Data::Grid::Container';
  1         1  
  1         60  
7              
8 1     1   5 use overload '@{}' => "cells";
  1         2  
  1         4  
9 1     1   48 use overload '%{}' => "as_hash";
  1         1  
  1         14  
10              
11             =head1 NAME
12              
13             Data::Grid::Row - Row implementation for Data::Grid::Table
14              
15             =head1 VERSION
16              
17             Version 0.01_01
18              
19             =cut
20              
21             our $VERSION = '0.01_01';
22              
23             =head1 SYNOPSIS
24              
25             # CSV files are themselves only a single table, so the list
26             # context assignment here takes the first and only one.
27             my ($table) = Data::Grid->parse('foo.csv')->tables;
28              
29             while (my $row = $table->next) {
30             my @cells = $row->cells;
31             # or
32             @cells = @$row;
33              
34             # or, if column names were supplied somehow:
35              
36             my %cells = $row->as_hash;
37             # or
38             %cells = %$row;
39             }
40              
41             =head1 METHODS
42              
43             =head2 table
44              
45             Retrieve the L object to which this row
46             belongs. Alias for L.
47              
48             =cut
49              
50             sub table {
51 1     1   76 no overload '%{}';
  1         2  
  1         5  
52 0     0 1   $_[0]->parent;
53             }
54              
55             =head2 cells
56              
57             Retrieve the cells from the row, as an array in list context or
58             arrayref in scalar context. The array dereferencing operator C<@{}> is
59             also overloaded and works like this:
60              
61             my @cells = @$row;
62              
63             =cut
64              
65             sub cells {
66 0     0 1   Carp::croak("This method is a stub; it must be overridden!");
67             }
68              
69             =head2 as_hash
70              
71             If the table has a heading or its columns were designated in the
72             constructor or with L, this method will
73             return the row as key-value pairs in list context and a HASH reference
74             in scalar context. If there is no column spec, this method will
75             generate dummy column names starting from 1, like C, C,
76             etc. It will also fill in the blanks if the column spec is shorter
77             than the actual row. If the column spec is longer, the overhang will
78             be populated with Cs. As well it is worth noting that duplicate
79             keys will be clobbered with the rightmost value at this time, though
80             that behaviour may change. As with the other pertinent methods, the
81             hash dereference operator C<%{}> is overloaded and will behave as
82             such:
83              
84             my %cells = %$row;
85              
86             =cut
87              
88             sub as_hash {
89 0     0 1   my $self = shift;
90 0 0         my @cols = $self->table->parent->fields or Carp::croak(
91             q{Can't make a hash of cells. The table must have a heading or },
92             q{the columns must be specified either in the constructor or by },
93             q{setting them with "columns" in Data::Grid::Table.});
94 0           my @cells = $self->cells;
95              
96 0           my %out;
97 0           for my $i (0..$#cols) {
98 0           $out{$cols[$i]} = $cells[$i];
99             }
100 0 0         wantarray ? %out : \%out;
101             }
102              
103             =head1 AUTHOR
104              
105             Dorian Taylor, C<< >>
106              
107             =head1 BUGS
108              
109             Please report any bugs or feature requests to C
110             rt.cpan.org>, or through the web interface at
111             L. I will
112             be notified, and then you'll automatically be notified of progress on
113             your bug as I make changes.
114              
115             =head1 SUPPORT
116              
117             You can find documentation for this module with the perldoc command.
118              
119             perldoc Data::Grid::Row
120              
121              
122             You can also look for information at:
123              
124             =over 4
125              
126             =item * RT: CPAN's request tracker
127              
128             L
129              
130             =item * AnnoCPAN: Annotated CPAN documentation
131              
132             L
133              
134             =item * CPAN Ratings
135              
136             L
137              
138             =item * Search CPAN
139              
140             L
141              
142             =back
143              
144             =head1 SEE ALSO
145              
146             L, L, L,
147             L
148              
149             =head1 LICENSE AND COPYRIGHT
150              
151             Copyright 2010 Dorian Taylor.
152              
153             This program is free software; you can redistribute it and/or modify it
154             under the terms of either: the GNU General Public License as published
155             by the Free Software Foundation; or the Artistic License.
156              
157             See http://dev.perl.org/licenses/ for more information.
158              
159              
160             =cut
161              
162             1; # End of Data::Grid::Row