| 1 | package Decl::Semantics::Data; | ||||||||||
| 2 | |||||||||||
| 3 | 13 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 13 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 13 | ▶ | use base qw(Decl::Node); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 13 | ▶ | use Text::ParseWords; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 13 | ▶ | use Iterator::Simple qw(:all); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 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 | 13 | 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<Text::ParseWords>). 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 | ▶ | sub defines { ('data'); } | ||||||||
|
Subroutine
defines: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 74 | 0 | ▶ | our %build_handlers = ( data => { node => sub { Decl::Semantics::Data->new (@_) }, body => 'none' } ); | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||
| 75 | 12 | ▶ | sub tags_defined { Decl->new_data(<<EOF); } | ||||||||
|
Subroutine
tags_defined: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 76 | data (body=text) | ||||||||||
| 77 | EOF | ||||||||||
| 78 | |||||||||||
| 79 | =head2 iterate | ||||||||||
| 80 | |||||||||||
| 81 | The C<iterate> 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<Text::ParseWords>. | ||||||||||
| 83 | |||||||||||
| 84 | =cut | ||||||||||
| 85 | |||||||||||
| 86 | 3 | ▶ | sub iterate { | ||||||||
|
Subroutine
iterate: called CC=1 SCAR=0.3
Pod
documented
| |||||||||||
| 87 | 3 | my $self = shift; | |||||||||
| 88 | |||||||||||
| 89 | 3 | my $iterator = $self->SUPER::iterate(); | |||||||||
| 90 | |||||||||||
| 91 | 12 | ▶ | iterator { | ||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=18.4
| |||||||||||
| 92 | 12 | my $next; | |||||||||
| 93 | 12 | ▶ | while (not $next = $iterator->next) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 94 | 3 | ▶ | return unless defined $next; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 95 | 0 | $next =~ s/\s+$//; | |||||||||
| 96 | 0 | ▶ | next unless $next; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 97 | } | ||||||||||
| 98 | 9 | ▶ | return unless defined $next; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 99 | 9 | my @words = parse_line('\s+', 0, $next); | |||||||||
| 100 | 9 | return \@words; | |||||||||
| 101 | } | ||||||||||
| 102 | 3 | } | |||||||||
| 103 | |||||||||||
| 104 | |||||||||||
| 105 | =head1 AUTHOR | ||||||||||
| 106 | |||||||||||
| 107 | Michael Roberts, C<< <michael at vivtek.com> >> | ||||||||||
| 108 | |||||||||||
| 109 | =head1 BUGS | ||||||||||
| 110 | |||||||||||
| 111 | Please report any bugs or feature requests to C<bug-decl at rt.cpan.org>, or through | ||||||||||
| 112 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Decl>. 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 | 13 | 1; # End of Decl::Semantics::Data | |||||||||