| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | # RDF::Trine::Exporter::CSV | 
| 2 |  |  |  |  |  |  | # ----------------------------------------------------------------------------- | 
| 3 |  |  |  |  |  |  |  | 
| 4 |  |  |  |  |  |  | =head1 NAME | 
| 5 |  |  |  |  |  |  |  | 
| 6 |  |  |  |  |  |  | RDF::Trine::Exporter::CSV - Export RDF data to CSV | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | =head1 VERSION | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | This document describes RDF::Trine::Exporter::CSV version 1.018 | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | use RDF::Trine::Exporter::CSV; | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | The RDF::Trine::Exporter::CSV class provides an API for serializing RDF data | 
| 19 |  |  |  |  |  |  | to CSV strings and files. | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | =cut | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | package RDF::Trine::Exporter::CSV; | 
| 24 |  |  |  |  |  |  |  | 
| 25 | 1 |  |  | 1 |  | 536 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 26 |  | 
| 26 | 1 |  |  | 1 |  | 5 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 23 |  | 
| 27 | 1 |  |  | 1 |  | 5 | no warnings 'redefine'; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 31 |  | 
| 28 |  |  |  |  |  |  |  | 
| 29 | 1 |  |  | 1 |  | 5 | use Data::Dumper; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 326 |  | 
| 30 | 1 |  |  | 1 |  | 714 | use Text::CSV_XS; | 
|  | 1 |  |  |  |  | 7140 |  | 
|  | 1 |  |  |  |  | 54 |  | 
| 31 | 1 |  |  | 1 |  | 8 | use Scalar::Util qw(blessed); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 49 |  | 
| 32 | 1 |  |  | 1 |  | 6 | use RDF::Trine::Error qw(:try); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 11 |  | 
| 33 |  |  |  |  |  |  |  | 
| 34 |  |  |  |  |  |  | our ($VERSION); | 
| 35 |  |  |  |  |  |  | BEGIN { | 
| 36 | 1 |  |  | 1 |  | 522 | $VERSION	= '1.018'; | 
| 37 |  |  |  |  |  |  | } | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | =head1 METHODS | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | =over 4 | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | =item C<< new ( sep_char => $sep_char, quote => $bool ) >> | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | Returns a new RDF::Trine::Exporter::CSV object. If C<< $sep_char >> is provided, | 
| 46 |  |  |  |  |  |  | it is used as the separator character in CSV serialization, otherwise a comma | 
| 47 |  |  |  |  |  |  | (",") is used. | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | =cut | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | sub new { | 
| 52 | 4 |  |  | 4 | 1 | 707 | my $class	= shift; | 
| 53 | 4 |  |  |  |  | 14 | my %args	= @_; | 
| 54 | 4 |  | 100 |  |  | 20 | my $sep		= $args{ sep_char } || ','; | 
| 55 | 4 |  |  |  |  | 8 | my $quote	= $args{ quote }; | 
| 56 | 4 |  |  |  |  | 28 | my $csv		= Text::CSV_XS->new ( { binary => 1, sep_char => $sep } ); | 
| 57 | 4 |  |  |  |  | 526 | my $self	= bless( { %args, csv => $csv }, $class ); | 
| 58 | 4 |  |  |  |  | 11 | return $self; | 
| 59 |  |  |  |  |  |  | } | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | =item C<< serialize_iterator_to_file ( $file, $iterator ) >> | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | Serializes the bindings objects produced by C<< $iterator >>, printing the | 
| 64 |  |  |  |  |  |  | results to the supplied filehandle C<<$fh>>. | 
| 65 |  |  |  |  |  |  |  | 
| 66 |  |  |  |  |  |  | =cut | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | sub serialize_iterator_to_file { | 
| 69 | 3 |  |  | 3 | 1 | 73 | my $self	= shift; | 
| 70 | 3 |  |  |  |  | 6 | my $file	= shift; | 
| 71 | 3 |  |  |  |  | 7 | my $iter	= shift; | 
| 72 |  |  |  |  |  |  |  | 
| 73 | 3 | 50 | 33 |  |  | 30 | unless (blessed($iter) and ($iter->isa('RDF::Trine::Iterator::Bindings') or $iter->isa('RDF::Trine::Iterator::Graph'))) { | 
|  |  |  | 33 |  |  |  |  | 
| 74 | 0 |  |  |  |  | 0 | my $type	= ref($iter); | 
| 75 | 0 |  |  |  |  | 0 | $type		=~ s/^RDF::Trine::Iterator:://; | 
| 76 | 0 |  |  |  |  | 0 | throw RDF::Trine::Error::MethodInvocationError -text => "CSV Exporter must be called with a Graph or VariableBindings iterator, not a $type iterator"; | 
| 77 |  |  |  |  |  |  | } | 
| 78 |  |  |  |  |  |  |  | 
| 79 | 3 | 50 |  |  |  | 11 | my $type	= ($iter->isa('RDF::Trine::Iterator::Bindings')) ? 'bindings' : 'graph'; | 
| 80 |  |  |  |  |  |  |  | 
| 81 | 3 |  |  |  |  | 13 | my $csv		= $self->{csv}; | 
| 82 | 3 |  |  |  |  | 6 | my $quote	= $self->{quote}; | 
| 83 | 3 |  |  |  |  | 4 | my @keys; | 
| 84 | 3 |  |  |  |  | 11 | while (my $row = $iter->next) { | 
| 85 | 9 | 100 |  |  |  | 22 | unless (scalar(@keys)) { | 
| 86 | 3 | 50 |  |  |  | 14 | @keys	= ($type eq 'bindings') ? (sort keys %$row) : qw(subject predicate object); | 
| 87 | 3 |  |  |  |  | 69 | $csv->print( $file, \@keys ); | 
| 88 | 3 |  |  |  |  | 44 | print {$file} "\n"; | 
|  | 3 |  |  |  |  | 7 |  | 
| 89 |  |  |  |  |  |  | } | 
| 90 | 9 |  |  |  |  | 18 | my @data; | 
| 91 | 9 |  |  |  |  | 17 | foreach my $k (@keys) { | 
| 92 | 18 | 50 |  |  |  | 44 | my $v	= ($type eq 'bindings') ? $row->{$k} : $row->$k(); | 
| 93 | 18 | 100 |  |  |  | 55 | if ($quote) { | 
|  |  | 50 |  |  |  |  |  | 
| 94 | 6 |  |  |  |  | 17 | push(@data, $v->as_string); | 
| 95 |  |  |  |  |  |  | } elsif (blessed($v)) { | 
| 96 | 12 | 100 |  |  |  | 62 | if ($v->isa('RDF::Trine::Node::Resource')) { | 
|  |  | 50 |  |  |  |  |  | 
|  |  | 50 |  |  |  |  |  | 
| 97 | 8 |  |  |  |  | 20 | push(@data, $v->uri_value); | 
| 98 |  |  |  |  |  |  | } elsif ($v->isa('RDF::Trine::Node::Blank')) { | 
| 99 | 0 |  |  |  |  | 0 | push(@data, $v->blank_identifier); | 
| 100 |  |  |  |  |  |  | } elsif ($v->isa('RDF::Trine::Node::Literal')) { | 
| 101 | 4 |  |  |  |  | 13 | push(@data, $v->literal_value); | 
| 102 |  |  |  |  |  |  | } | 
| 103 |  |  |  |  |  |  | } else { | 
| 104 | 0 |  |  |  |  | 0 | push(@data, ''); | 
| 105 |  |  |  |  |  |  | } | 
| 106 |  |  |  |  |  |  | } | 
| 107 | 9 |  |  |  |  | 53 | $csv->print( $file, \@data ); | 
| 108 | 9 |  |  |  |  | 64 | print {$file} "\n"; | 
|  | 9 |  |  |  |  | 35 |  | 
| 109 |  |  |  |  |  |  | } | 
| 110 |  |  |  |  |  |  | } | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | =item C<< serialize_iterator_to_string ( $iterator ) >> | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | Serializes the bindings objects produced by C<< $iterator >>, returning the | 
| 115 |  |  |  |  |  |  | result as a string. | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | =cut | 
| 118 |  |  |  |  |  |  |  | 
| 119 |  |  |  |  |  |  | sub serialize_iterator_to_string { | 
| 120 | 1 |  |  | 1 | 1 | 7 | my $self	= shift; | 
| 121 | 1 |  |  |  |  | 2 | my $iter	= shift; | 
| 122 | 1 |  |  |  |  | 3 | my $string	= ''; | 
| 123 | 1 |  |  | 1 |  | 9 | open( my $fh, '>', \$string ); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 8 |  | 
|  | 1 |  |  |  |  | 41 |  | 
| 124 | 1 |  |  |  |  | 829 | $self->serialize_iterator_to_file( $fh, $iter ); | 
| 125 | 1 |  |  |  |  | 4 | close($fh); | 
| 126 | 1 |  |  |  |  | 5 | return $string; | 
| 127 |  |  |  |  |  |  | } | 
| 128 |  |  |  |  |  |  |  | 
| 129 |  |  |  |  |  |  | 1; | 
| 130 |  |  |  |  |  |  |  | 
| 131 |  |  |  |  |  |  | __END__ | 
| 132 |  |  |  |  |  |  |  | 
| 133 |  |  |  |  |  |  | =back | 
| 134 |  |  |  |  |  |  |  | 
| 135 |  |  |  |  |  |  | =head1 BUGS | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | Please report any bugs or feature requests to through the GitHub web interface | 
| 138 |  |  |  |  |  |  | at L<https://github.com/kasei/perlrdf/issues>. | 
| 139 |  |  |  |  |  |  |  | 
| 140 |  |  |  |  |  |  | =head1 AUTHOR | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  | Gregory Todd Williams  C<< <gwilliams@cpan.org> >> | 
| 143 |  |  |  |  |  |  |  | 
| 144 |  |  |  |  |  |  | =head1 COPYRIGHT | 
| 145 |  |  |  |  |  |  |  | 
| 146 |  |  |  |  |  |  | Copyright (c) 2006-2012 Gregory Todd Williams. This | 
| 147 |  |  |  |  |  |  | program is free software; you can redistribute it and/or modify it under | 
| 148 |  |  |  |  |  |  | the same terms as Perl itself. | 
| 149 |  |  |  |  |  |  |  | 
| 150 |  |  |  |  |  |  | =cut |