| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | =head1 NAME | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | AtteanX::Serializer::SPARQLCSV - SPARQL Results CSV Serializer | 
| 4 |  |  |  |  |  |  |  | 
| 5 |  |  |  |  |  |  | =head1 VERSION | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | This document describes AtteanX::Serializer::SPARQLCSV version 0.032 | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | use Attean; | 
| 12 |  |  |  |  |  |  | my $s = Attean->get_serializer('SPARQLCSV')->new(); | 
| 13 |  |  |  |  |  |  | $s->serialize_iter_to_io( $fh, $iter ); | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | ... | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | =head1 ATTRIBUTES | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | =over 4 | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | =item C<< canonical_media_type >> | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | =item C<< file_extensions >> | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | =back | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | =head1 METHODS | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | =over 4 | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | =cut | 
| 34 |  |  |  |  |  |  |  | 
| 35 | 4 |  |  | 4 |  | 12094 | use v5.14; | 
|  | 4 |  |  |  |  | 17 |  | 
| 36 | 4 |  |  | 4 |  | 21 | use warnings; | 
|  | 4 |  |  |  |  | 12 |  | 
|  | 4 |  |  |  |  | 175 |  | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | use Moo; | 
| 39 | 4 |  |  | 4 |  | 21 | use Types::Standard qw(Str ArrayRef); | 
|  | 4 |  |  |  |  | 13 |  | 
|  | 4 |  |  |  |  | 36 |  | 
| 40 | 4 |  |  | 4 |  | 1343 | use Encode qw(encode); | 
|  | 4 |  |  |  |  | 8 |  | 
|  | 4 |  |  |  |  | 43 |  | 
| 41 | 4 |  |  | 4 |  | 2878 | use Scalar::Util qw(blessed); | 
|  | 4 |  |  |  |  | 11 |  | 
|  | 4 |  |  |  |  | 212 |  | 
| 42 | 4 |  |  | 4 |  | 25 | use Attean::ListIterator; | 
|  | 4 |  |  |  |  | 13 |  | 
|  | 4 |  |  |  |  | 142 |  | 
| 43 | 4 |  |  | 4 |  | 23 | use List::MoreUtils qw(any); | 
|  | 4 |  |  |  |  | 10 |  | 
|  | 4 |  |  |  |  | 108 |  | 
| 44 | 4 |  |  | 4 |  | 22 | use Text::CSV; | 
|  | 4 |  |  |  |  | 8 |  | 
|  | 4 |  |  |  |  | 34 |  | 
| 45 | 4 |  |  | 4 |  | 6677 | use namespace::clean; | 
|  | 4 |  |  |  |  | 53326 |  | 
|  | 4 |  |  |  |  | 163 |  | 
| 46 | 4 |  |  | 4 |  | 31 |  | 
|  | 4 |  |  |  |  | 10 |  | 
|  | 4 |  |  |  |  | 37 |  | 
| 47 |  |  |  |  |  |  | has 'canonical_media_type' => (is => 'ro', isa => Str, init_arg => undef, default => 'text/csv'); | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | =item C<< media_types >> | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | Returns a list of media types that identify the format produced by this serializer. | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | =cut | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | return [qw(text/csv)]; | 
| 56 |  |  |  |  |  |  | } | 
| 57 | 10 |  |  | 10 | 1 | 25 |  | 
| 58 |  |  |  |  |  |  | =item C<< file_extensions >> | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | Returns a list of file extensions associated with the serialized format. | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | =cut | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | =item C<< serialize_iter_to_io( $fh, $iterator ) >> | 
| 66 | 10 |  |  | 10 | 1 | 42 |  | 
| 67 |  |  |  |  |  |  | Serializes the L<Attean::API::Binding> objects from C<< $iterator >> to the | 
| 68 |  |  |  |  |  |  | L<IO::Handle> object C<< $fh >>. | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | =cut | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | my $self	= shift; | 
| 73 |  |  |  |  |  |  | my $io		= shift; | 
| 74 |  |  |  |  |  |  | my $iter	= shift; | 
| 75 |  |  |  |  |  |  | my $csv = Text::CSV->new ( { binary => 1 } ); | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | my @vars	= @{ $iter->variables }; | 
| 78 |  |  |  |  |  |  | $csv->print($io, \@vars); | 
| 79 |  |  |  |  |  |  | print $io "\n"; | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | while (my $t = $iter->next()) { | 
| 82 |  |  |  |  |  |  | my @strings; | 
| 83 |  |  |  |  |  |  | foreach my $var (@vars) { | 
| 84 |  |  |  |  |  |  | my $term	= $t->value($var); | 
| 85 |  |  |  |  |  |  | if (blessed($term)) { | 
| 86 |  |  |  |  |  |  | if ($term->does('Attean::API::Blank')) { | 
| 87 |  |  |  |  |  |  | push(@strings, $term->ntriples_string); | 
| 88 |  |  |  |  |  |  | } else { | 
| 89 |  |  |  |  |  |  | push(@strings, $term->value); | 
| 90 |  |  |  |  |  |  | } | 
| 91 |  |  |  |  |  |  | } else { | 
| 92 |  |  |  |  |  |  | push(@strings, ''); | 
| 93 |  |  |  |  |  |  | } | 
| 94 |  |  |  |  |  |  | } | 
| 95 |  |  |  |  |  |  | $csv->print($io, [@strings]); | 
| 96 |  |  |  |  |  |  | print $io "\n"; | 
| 97 |  |  |  |  |  |  | } | 
| 98 |  |  |  |  |  |  | return; | 
| 99 |  |  |  |  |  |  | } | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | =item C<< serialize_iter_to_bytes( $iterator ) >> | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  | Serializes the L<Attean::API::Binding> objects from C<< $iterator >> | 
| 104 |  |  |  |  |  |  | and returns the serialization as a UTF-8 encoded byte string. | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | =cut | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  | my $self	= shift; | 
| 109 |  |  |  |  |  |  | my $iter	= shift; | 
| 110 |  |  |  |  |  |  | my $data	= encode('UTF-8', ''); | 
| 111 |  |  |  |  |  |  | my $csv = Text::CSV->new ( { binary => 1 } ); | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | my @vars	= @{ $iter->variables }; | 
| 114 |  |  |  |  |  |  | $csv->combine(map { encode('UTF-8', $_) } @vars); | 
| 115 |  |  |  |  |  |  | $data		.= $csv->string . "\n"; | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | while (my $t = $iter->next()) { | 
| 118 |  |  |  |  |  |  | my @strings; | 
| 119 |  |  |  |  |  |  | foreach my $var (@vars) { | 
| 120 |  |  |  |  |  |  | my $term	= $t->value($var); | 
| 121 |  |  |  |  |  |  | if (blessed($term)) { | 
| 122 |  |  |  |  |  |  | if ($term->does('Attean::API::Blank')) { | 
| 123 |  |  |  |  |  |  | push(@strings, $term->ntriples_string); | 
| 124 |  |  |  |  |  |  | } else { | 
| 125 |  |  |  |  |  |  | push(@strings, $term->value); | 
| 126 |  |  |  |  |  |  | } | 
| 127 |  |  |  |  |  |  | } else { | 
| 128 |  |  |  |  |  |  | push(@strings, ''); | 
| 129 |  |  |  |  |  |  | } | 
| 130 |  |  |  |  |  |  | } | 
| 131 |  |  |  |  |  |  | if ($csv->combine(map { encode('UTF-8', $_) } @strings)) { | 
| 132 |  |  |  |  |  |  | $data	.= $csv->string . "\n"; | 
| 133 |  |  |  |  |  |  | } | 
| 134 |  |  |  |  |  |  | } | 
| 135 |  |  |  |  |  |  | return $data; | 
| 136 |  |  |  |  |  |  | } | 
| 137 |  |  |  |  |  |  |  | 
| 138 |  |  |  |  |  |  | with 'Attean::API::ResultSerializer', 'Attean::API::AppendableSerializer'; | 
| 139 |  |  |  |  |  |  | } | 
| 140 |  |  |  |  |  |  |  | 
| 141 |  |  |  |  |  |  | 1; | 
| 142 |  |  |  |  |  |  |  | 
| 143 |  |  |  |  |  |  |  | 
| 144 |  |  |  |  |  |  | =back | 
| 145 |  |  |  |  |  |  |  | 
| 146 |  |  |  |  |  |  | =head1 BUGS | 
| 147 |  |  |  |  |  |  |  | 
| 148 |  |  |  |  |  |  | Please report any bugs or feature requests to through the GitHub web interface | 
| 149 |  |  |  |  |  |  | at L<https://github.com/kasei/perlrdf/issues>. | 
| 150 |  |  |  |  |  |  |  | 
| 151 |  |  |  |  |  |  | =head1 AUTHOR | 
| 152 |  |  |  |  |  |  |  | 
| 153 |  |  |  |  |  |  | Gregory Todd Williams  C<< <gwilliams@cpan.org> >> | 
| 154 |  |  |  |  |  |  |  | 
| 155 |  |  |  |  |  |  | =head1 COPYRIGHT | 
| 156 |  |  |  |  |  |  |  | 
| 157 |  |  |  |  |  |  | Copyright (c) 2014--2022 Gregory Todd Williams. This | 
| 158 |  |  |  |  |  |  | program is free software; you can redistribute it and/or modify it under | 
| 159 |  |  |  |  |  |  | the same terms as Perl itself. | 
| 160 |  |  |  |  |  |  |  | 
| 161 |  |  |  |  |  |  | =cut |