AnyData/Format/HTMLtable.pm

filter: Statementstmt 60.5% 46 / 76 Branchbran 6.2% 1 / 16 Conditioncond 19.3% 6 / 31 MC/DCmcdc 6.6% 1 / 15 Subroutinesub 76.9% 10 / 13 Podpod 0.0% 0 / 5 totaltotal 41.0% 64 / 156 CC 23 SCAR 44.6
CC
23
Cov
43%
get_data
210.0
exportOLD
12.6
export
6.1
CRAP 86.9
« AnyData/Format/XML.pm ↑ Summary AnyData/Storage/RAM.pm »
1 ######################################################################
2 package AnyData::Format::HTMLtable;
3 ######################################################################
4 # by Jeff Zucker <jeff@vpservices.com>
5 # copyright 2000 all rights reserved
6 ######################################################################
7  
8 =head1 NAME
9  
10 HTMLtable - tied hash and DBI/SQL access to HTML tables
11  
12 =head1 SYNOPSIS
13  
14 use AnyData;
15 my $table = adHash( 'HTMLtable', $filename );
16 while (my $row = each %$table) {
17 print $row->{name},"\n" if $row->{country} =~ /us|mx|ca/;
18 }
19 # ... other tied hash operations
20  
21 OR
22  
23 use DBI
24 my $dbh = DBI->connect('dbi:AnyData:');
25 $dbh->func('table1','HTMLtable', $filename,'ad_catalog');
26 my $hits = $dbh->selectall_arrayref( qq{
27 SELECT name FROM table1 WHERE country = 'us'
28 });
29 # ... other DBI/SQL operations
30  
31  
32 =head1 DESCRIPTION
33  
34 This module allows one to treat the data contained in an HTML table as
35 a tied hash (using AnyData.pm) or as a DBI/SQL accessible database
36 (using DBD::AnyData.pm). Both the tiedhash and DBI interfaces allow
37 one to read, modify, and create HTML tables from perl data or from local or
38 remote files.
39  
40 The module requires that CGI, HTML::Parser and HTML::TableExtract are installed.
41  
42 When reading the HTML table, this module is essentially just a pass
43 through to Matt Sisk's excellent HTML::TableExtract module.
44  
45 If no flags are specified in the adTie() or ad_catalog() calls, then TableExtract is called with depth=0 and count=0, in other words it finds the first row of the first table and treats that as the column names for the entire table. If a flag for 'cols' (column names) is specified in the adTie() or ad_catalog() calls, that list of column names is passed to TableExtract as a headers parameter. If the user specifies flags for headers, depth, or count, those are passed directly to TableExtract.
46  
47 When exporting to an HTMLtable, you may pass flags to specify properties
48 of the whole table (table_flags), the top row containing the column names
49 (top_row_flags), and the data rows (data_row_flags). These flags follow
50 the syntax of CGI.pm table constructors, e.g.:
51  
52 print adExport( $table, 'HTMLtable', {
53 table_flags => {Border=>3,bgColor=>'blue'};
54 top_row_flags => {bgColor=>'red'};
55 data_row_flags => {valign='top'};
56 });
57  
58 The table_flags will default to {Border=>1,bgColor=>'white'} if none
59 are specified.
60  
61 The top_row_flags will default to {bgColor=>'#c0c0c0'} if none are
62 specified;
63  
64 The data_row_flags will be empty if none are specified.
65  
66 In other words, if no flags are specified the table will print out with
67 a border of 1, the column headings in gray, and the data rows in white.
68  
69 CAUTION: This module will *not* preserve anything in the html file except
70 the selected table so if your file contains more than the selected table,
71 you will want to use adTie() or $dbh->func(...,'ad_import') to read the
72 table and then adExport() or $dbh->func(...,'ad_export') to write
73 the table to a different file. When using the HTMLtable format, this is the
74 only way to preserve changes to the data, the adTie() command will *not*
75 write to a file.
76  
77 =head1 AUTHOR & COPYRIGHT
78  
79 copyright 2000, Jeff Zucker <jeff@vpservices.com>
80 all rights reserved
81  
82 =cut
83  
84 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
85 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
86 1 ▶use AnyData::Format::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
87 1 ▶use AnyData::Storage::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
88 1 ▶use vars qw( @ISA $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
89 1 @AnyData::Format::HTMLtable::ISA = qw( AnyData::Format::Base );
90  
91 1 $VERSION = '0.12';
92  
93 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
94 1 my $class = shift;
95 1 ▶ my $self = shift || {};
Condition50%1/2
shift() || {}
l!l
10
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
96 1 $self->{export_on_close} = 1;
97 1 $self->{slurp_mode} = 1;
98 1 return bless $self, $class;
99 }
100  
101 1 ▶sub storage_type { 'RAM'; }
Subroutine storage_type: called CC=1 SCAR=0.0
Pod undocumented
102  
103 0 ▶sub import {
Subroutine import: not called CC=1 SCAR=6.9
104 0 my $self = shift;
105 0 my $data = shift;
106 0 my $storage = shift;
107 0 return $self->get_data($data,$self->{col_names});
108 }
109 0 ▶sub get_data {
Subroutine get_data: not called CC=14 SCAR=53.5
Pod undocumented
110 0 my $self = shift;
111 0 ▶ my $str = shift or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $str = shift() or return undef 0 0 0
112 0 my $col_names = shift;
113 0 require HTML::TableExtract;
114 0 ▶ my $count = $self->{count} || 0;
Condition0%0/2
$self->{'count'} || 0
l!l
00
MC/DC0%0/1
$self->{'count'} || 0
$self->{'count'}
Truth table0%0/2
A
A= $self->{'count'}
Aresult
11
00
115 0 ▶ my $depth = $self->{depth} || 0;
Condition0%0/2
$self->{'depth'} || 0
l!l
00
MC/DC0%0/1
$self->{'depth'} || 0
$self->{'depth'}
Truth table0%0/2
A
A= $self->{'depth'}
Aresult
11
00
116 0 ▶ my $headers = $self->{headers} || $self->{col_names} || undef;
Condition0%0/5
$self->{'headers'} || $self->{'col_names'} || undef
l!l
00
$self->{"headers"} or $self->{"col_names"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'headers'} || $self->{'col_names'} || undef
$self->{"headers"} $self->{"col_names"}
Truth table0%0/3
A or B
A= $self->{"headers"}B= $self->{"col_names"}
ABresult
1X1
011
000
117 0 my %flags;
118 0 ▶ if (defined $count or defined $depth or defined $headers) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $count or defined $depth or defined $headers) { } 0 0 0
Condition0%0/6
defined $count or defined $depth or defined $headers
l!l&&r!l&&!r
000
defined $count or defined $depth
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $count or defined $depth or defined $headers
defined $count defined $depth defined $headers
Truth table0%0/4
A or B or C
A= defined $countB= defined $depthC= defined $headers
ABCresult
1XX1
01X1
0011
0000
119 0 ▶ $flags{count} = $count if defined $count;
Branch: 1 branch
BranchTrueFalseTotal
if defined $count 0 0 0
120 0 ▶ $flags{depth} = $depth if defined $depth;
Branch: 1 branch
BranchTrueFalseTotal
if defined $depth 0 0 0
121 0 ▶ $flags{headers} = $headers if defined $headers;
Branch: 1 branch
BranchTrueFalseTotal
if defined $headers 0 0 0
122 }
123 else {
124 0 ▶ %flags = $col_names
Branch: 1 branch
BranchTrueFalseTotal
$col_names ? : 0 0 0
125 ? ( headers => $col_names )
126 : (count=>$count,depth=>$depth);
127 }
128 0 my $te = new HTML::TableExtract(
129 %flags
130 );
131 0 $te->parse($str);
132 0 my $table;
133 0 @$table = $te->rows;
134 0 ▶ $self->{col_names} = shift @$table if !$col_names;
Branch: 1 branch
BranchTrueFalseTotal
unless $col_names 0 0 0
135 0 return $table, $self->{col_names};
136 }
137  
138 1 ▶sub export {
Subroutine export: called CC=6 SCAR=18.1
Pod undocumented
139 #print "EXPORTING!";
140 1 my $self = shift;
141 1 my $storage = shift;
142 1 my $col_names = $storage->{col_names};
143 1 my $table = $storage->{records};
144 #use Data::Dumper; print Dumper $table; print "###"; exit;
145 1 my $fh = $storage->{fh};
146 1 ▶ use CGI;
Subroutine BEGIN: called CC=1 SCAR=0.0
147 1 ▶ my $table_flags = shift || {Border=>1,bgColor=>'white'};
Condition50%1/2
shift() || {'Border', 1, 'bgColor', 'white'}
l!l
10
MC/DC0%0/1
shift() || {'Border', 1, 'bgColor', 'white'}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
148 1 ▶ my $top_row_flags = shift || {bgColor=>'#c0c0c0'};
Condition50%1/2
shift() || {'bgColor', '#c0c0c0'}
l!l
01
MC/DC0%0/1
shift() || {'bgColor', '#c0c0c0'}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
149 1 ▶ my $data_row_flags = shift || {};
Condition50%1/2
shift() || {}
l!l
01
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
150 @$table = map {
151 1 my $row = $_;
152 6 ▶ @$row = map { $_ || '&nbsp;' } @$row;
Condition100%2/2
$_ || '&nbsp;'
l!l
92
MC/DC100%1/1
$_ || '&nbsp;'
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
153 6 $row;
154 } @$table;
155 1 my $str =
156 CGI::table(
157 $table_flags,
158 CGI::Tr( $top_row_flags, CGI::th($col_names) ),
159 map CGI::Tr( $data_row_flags, CGI::td($_) ), @$table
160 );
161 1 ▶ $fh->write($str,length $str) if $fh;
Branch: 1 branch
BranchTrueFalseTotal
if $fh 1 0 1
162 1 return $str;
163 }
164  
165 0 ▶sub exportOLD {
Subroutine exportOLD: not called CC=4 SCAR=25.3
Pod undocumented
166 0 my $self = shift;
167 0 my $table = shift;
168 0 my $col_names = shift;
169 1 ▶ use CGI;
Subroutine BEGIN: called CC=1 SCAR=0.0
170 0 ▶ my $table_flags = shift || {Border=>1,bgColor=>'white'};
Condition0%0/2
shift() || {'Border', 1, 'bgColor', 'white'}
l!l
00
MC/DC0%0/1
shift() || {'Border', 1, 'bgColor', 'white'}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
171 0 ▶ my $top_row_flags = shift || {bgColor=>'#c0c0c0'};
Condition0%0/2
shift() || {'bgColor', '#c0c0c0'}
l!l
00
MC/DC0%0/1
shift() || {'bgColor', '#c0c0c0'}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
172 0 ▶ my $data_row_flags = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
173 return
174 0 CGI::table(
175 $table_flags,
176 CGI::Tr( $top_row_flags, CGI::th($col_names) ),
177 map CGI::Tr( $data_row_flags, CGI::td($_) ), @$table
178 );
179 }
180 1 1;
181  
182  
183  
184  
185  
186  
187  
188  
« AnyData/Format/XML.pm ↑ Summary AnyData/Storage/RAM.pm »