/
CSV.pm
136 lines (101 loc) · 2.77 KB
/
CSV.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# RDF::Trine::Exporter::CSV
# -----------------------------------------------------------------------------
=head1 NAME
RDF::Trine::Exporter::CSV - Export RDF data to CSV.
=head1 VERSION
This document describes RDF::Trine::Exporter::CSV version 0.120
=head1 SYNOPSIS
use RDF::Trine::Exporter::CSV;
=head1 DESCRIPTION
The RDF::Trine::Exporter::CSV class provides an API for serializing RDF data
to CSV strings and files.
=cut
package RDF::Trine::Exporter::CSV;
use strict;
use warnings;
no warnings 'redefine';
use Data::Dumper;
use Text::CSV;
use Scalar::Util qw(blessed);
use RDF::Trine::Error qw(:try);
our ($VERSION);
BEGIN {
$VERSION = '0.120';
}
=head1 METHODS
=over 4
=item C<< new ( sep_char => $sep_char, quote => $bool ) >>
Returns a new RDF::Trine::Exporter::CSV object. If C<< $sep_char >> is provided,
it is used as the seperator character in CSV serialization, otherwise a comma
(",") is used.
=cut
sub new {
my $class = shift;
my %args = @_;
my $sep = $args{ sep_char } || ',';
my $quote = $args{ quote };
my $csv = Text::CSV->new ( { binary => 1, sep_char => $sep } );
my $self = bless( { %args, csv => $csv }, $class );
return $self;
}
=item C<< serialize_iterator_to_file ( $file, $iterator ) >>
Serializes the bindings objects produced by C<< $iterator >>, printing the
results to the supplied filehandle C<<$fh>>.
=cut
sub serialize_iterator_to_file {
my $self = shift;
my $file = shift;
my $iter = shift;
my $csv = $self->{csv};
my $quote = $self->{quote};
my @keys;
while (my $row = $iter->next) {
unless (scalar(@keys)) {
@keys = keys %$row;
$csv->print( $file, \@keys );
print {$file} "\n";
}
my @data;
foreach my $k (@keys) {
my $v = $row->{$k};
if ($quote) {
push(@data, $v->as_string);
} elsif (blessed($v)) {
if ($v->isa('RDF::Trine::Node::Resource')) {
push(@data, $v->uri_value);
} elsif ($v->isa('RDF::Trine::Node::Blank')) {
push(@data, $v->blank_identifier);
} elsif ($v->isa('RDF::Trine::Node::Literal')) {
push(@data, $v->literal_value);
}
} else {
push(@data, '');
}
}
$csv->print( $file, \@data );
print {$file} "\n";
}
}
=item C<< serialize_iterator_to_string ( $iterator ) >>
Serializes the bindings objects produced by C<< $iterator >>, returning the
result as a string.
=cut
sub serialize_iterator_to_string {
my $self = shift;
my $iter = shift;
my $string = '';
open( my $fh, '>', \$string );
$self->serialize_iterator_to_file( $fh, $iter );
close($fh);
return $string;
}
1;
__END__
=back
=head1 AUTHOR
Gregory Todd Williams C<< <gwilliams@cpan.org> >>
=head1 COPYRIGHT
Copyright (c) 2006-2010 Gregory Todd Williams. All rights reserved. This
program is free software; you can redistribute it and/or modify it under
the same terms as Perl itself.
=cut