File Coverage

lib/Decl/Semantics/Data.pm
Criterion Covered Total %
statement 25 28 89.2
branch 2 6 33.3
condition n/a
subroutine 8 9 88.8
pod 3 3 100.0
total 38 46 82.6


line stmt bran cond sub pod time code
1             package Decl::Semantics::Data;
2            
3 12     12   71 use warnings;
  12         31  
  12         341  
4 12     12   75 use strict;
  12         26  
  12         355  
5            
6 12     12   61 use base qw(Decl::Node);
  12         24  
  12         1372  
7 12     12   76 use Text::ParseWords;
  12         22  
  12         1037  
8 12     12   70 use Iterator::Simple qw(:all);
  12         21  
  12         6525  
9            
10             =head1 NAME
11            
12             Decl::Semantics::Data - implements a data table.
13            
14             =head1 VERSION
15            
16             Version 0.01
17            
18             =cut
19            
20             our $VERSION = '0.01';
21            
22            
23             =head1 SYNOPSIS
24            
25             The data class implements tabular data. This is different from the definition of the table itself, which will be used
26             (later) to identify external data sources. Tabular data can be seen as a memory table or an in-memory cursor for ongoing tabular data manipulation.
27            
28             Note also that *tree* data can be retrieved any old time, as it's inherent in the structure of our declarative language. Explicitly declared tabular
29             data is really just to save some typing and for the sake of some efficiency in retrieval.
30            
31             Tabular data is space-delimited and obeys quotes in the same way as the Unix shell (it uses L). For instance:
32            
33             data (field1, field2, field3)
34             value1 "first row" 3
35             value2 "second row" 19
36             value3 "third row" 20
37            
38             An iterator retrieved for this field would return three values (the names of the fields are for other people to know what we've got):
39            
40             ['value1', 'first row', 3]
41             ['value2', 'second row', 19]
42             ['value3', 'third row', 20]
43            
44             The data tag can also have a code body that returns such an iterator; really, the data tag is just a holder for an iterator that returns lists of
45             data, in the same way that the text tag is a holder for an iterator that returns strings. (And any node can be seen as an iterator that returns other nodes.)
46            
47             The "foreach" command writes code appropriate to a given iterator, so for the example above, we might embed this:
48            
49             ^foreach field1, field2 in data {
50             print "$field1: $field2\n";
51             }
52            
53             This would translate into something like this Perl code:
54            
55             my @data8792 = $self->find_data("data");
56             my $i8792 = $data8792[0]->iterate;
57             while ($row8792 = $i8792->next) {
58             my ($field1, $field2) = @$row8792;
59            
60             print "$field1: $field2\n";
61             }
62            
63             (Except all that generated stuff would be on one line, to avoid confusing warning messages if and when we get around to intercepting warning messages.)
64            
65             For other types of iterator, it would do something slightly different.
66            
67            
68             =head2 defines(), tags_defined()
69            
70             Called by Decl::Semantics during import, to find out what xmlapi tags this plugin claims to implement.
71            
72             =cut
73 0     0 1 0 sub defines { ('data'); }
74             our %build_handlers = ( data => { node => sub { Decl::Semantics::Data->new (@_) }, body => 'none' } );
75 12     12 1 71 sub tags_defined { Decl->new_data(<
76             data (body=text)
77             EOF
78            
79             =head2 iterate
80            
81             The C function is called when we want to iterate over the contents of the data node. It inherits from the generic line-by-line iteration
82             functionality of the Node class, but splits its lines into words using L.
83            
84             =cut
85            
86             sub iterate {
87 3     3 1 12 my $self = shift;
88            
89 3         15 my $iterator = $self->SUPER::iterate();
90            
91             iterator {
92 12     12   72 my $next;
93 12         36 while (not $next = $iterator->next) {
94 3 50       82 return unless defined $next;
95 0         0 $next =~ s/\s+$//;
96 0 0       0 next unless $next;
97             }
98 9 50       91 return unless defined $next;
99 9         33 my @words = parse_line('\s+', 0, $next);
100 9         1761 return \@words;
101             }
102 3         91 }
103            
104            
105             =head1 AUTHOR
106            
107             Michael Roberts, C<< >>
108            
109             =head1 BUGS
110            
111             Please report any bugs or feature requests to C, or through
112             the web interface at L. I will be notified, and then you'll
113             automatically be notified of progress on your bug as I make changes.
114            
115             =head1 LICENSE AND COPYRIGHT
116            
117             Copyright 2010 Michael Roberts.
118            
119             This program is free software; you can redistribute it and/or modify it
120             under the terms of either: the GNU General Public License as published
121             by the Free Software Foundation; or the Artistic License.
122            
123             See http://dev.perl.org/licenses/ for more information.
124            
125             =cut
126            
127             1; # End of Decl::Semantics::Data