/usr/share/perl5/RDF/Trine/Exporter/CSV.pm is in librdf-trine-perl 1.019-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | # 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 1.019
=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_XS;
use Scalar::Util qw(blessed);
use RDF::Trine::Error qw(:try);
our ($VERSION);
BEGIN {
$VERSION = '1.019';
}
=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 separator 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_XS->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;
unless (blessed($iter) and ($iter->isa('RDF::Trine::Iterator::Bindings') or $iter->isa('RDF::Trine::Iterator::Graph'))) {
my $type = ref($iter);
$type =~ s/^RDF::Trine::Iterator:://;
throw RDF::Trine::Error::MethodInvocationError -text => "CSV Exporter must be called with a Graph or VariableBindings iterator, not a $type iterator";
}
my $type = ($iter->isa('RDF::Trine::Iterator::Bindings')) ? 'bindings' : 'graph';
my $csv = $self->{csv};
my $quote = $self->{quote};
my @keys;
while (my $row = $iter->next) {
unless (scalar(@keys)) {
@keys = ($type eq 'bindings') ? (sort keys %$row) : qw(subject predicate object);
$csv->print( $file, \@keys );
print {$file} "\n";
}
my @data;
foreach my $k (@keys) {
my $v = ($type eq 'bindings') ? $row->{$k} : $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 BUGS
Please report any bugs or feature requests to through the GitHub web interface
at L<https://github.com/kasei/perlrdf/issues>.
=head1 AUTHOR
Gregory Todd Williams C<< <gwilliams@cpan.org> >>
=head1 COPYRIGHT
Copyright (c) 2006-2012 Gregory Todd Williams. This
program is free software; you can redistribute it and/or modify it under
the same terms as Perl itself.
=cut
|