| 1 |
NAME |
| 2 |
RDF::TrineX::Functions - some shortcut functions for RDF::Trine's |
| 3 |
object-oriented interface |
| 4 |
|
| 5 |
SYNOPSIS |
| 6 |
use RDF::TrineX::Functions -all; |
| 7 |
|
| 8 |
my $model = model(); |
| 9 |
parse('/tmp/mydata.rdf', into => $model); |
| 10 |
|
| 11 |
$model->add_statement(statement( |
| 12 |
iri('http://example.com/'), |
| 13 |
iri('http://purl.org/dc/terms/title'), |
| 14 |
"An Example", |
| 15 |
)); |
| 16 |
|
| 17 |
print RDF::Trine::Serializer |
| 18 |
-> new('Turtle') |
| 19 |
-> serialize_model_to_string($model); |
| 20 |
|
| 21 |
DESCRIPTION |
| 22 |
This is a replacement for the venerable RDF::TrineShortcuts. Not a drop-in |
| 23 |
replacement. It has fewer features, fewer dependencies, less hackishness, |
| 24 |
less magic and fewer places it can go wrong. |
| 25 |
|
| 26 |
It uses Sub::Exporter, which allows exported functions to be renamed |
| 27 |
easily: |
| 28 |
|
| 29 |
use RDF::TrineX::Functions |
| 30 |
parse => { -as => 'parse_rdf' }; |
| 31 |
|
| 32 |
Functions |
| 33 |
`iri`, `literal`, `blank`, `variable` |
| 34 |
As per the similarly named functions exported by RDF::Trine itself. |
| 35 |
|
| 36 |
These are wrapped with a very tiny bit of DWIMmery. A blessed URI |
| 37 |
object passed to `iri` will be handled properly; a blessed URI object |
| 38 |
passed to `literal` will default the datatype to xsd:anyURI. A string |
| 39 |
starting with "_:" passed to either `iri` or `blank` will correctly |
| 40 |
create a blank node. A string starting with "?" passed to either |
| 41 |
`blank` or `variable` will correctly create a variable. If any of them |
| 42 |
are passed an existing RDF::Trine::Node, it will be passed through |
| 43 |
untouched. |
| 44 |
|
| 45 |
Other than that, no magic. |
| 46 |
|
| 47 |
`curie` |
| 48 |
Like `iri` but passes strings through RDF::NS::Trine. |
| 49 |
|
| 50 |
`statement(@nodes)` |
| 51 |
As per the similarly named function exported by RDF::Trine itself. |
| 52 |
|
| 53 |
Again, a tiny bit of DWIMmery: blessed URI objects are passed through |
| 54 |
`iri` and unblessed scalars (i.e. strings) are assumed to be literals. |
| 55 |
|
| 56 |
`store` |
| 57 |
As per the similarly named function exported by RDF::Trine itself. |
| 58 |
|
| 59 |
`model` |
| 60 |
Returns a new RDF::Trine::Model. May be passed a store as a parameter. |
| 61 |
|
| 62 |
`parse($source, %options)` |
| 63 |
Parses the source and returns an RDF::Trine::Model. The source may be: |
| 64 |
|
| 65 |
* a URI |
| 66 |
|
| 67 |
A string URI, blessed URI object or RDF::Trine::Node::Resource, |
| 68 |
which will be retrieved and parsed. |
| 69 |
|
| 70 |
* a file |
| 71 |
|
| 72 |
A filehandle, Path::Class::File, IO::All, IO::Handle object, or |
| 73 |
the name of an existing file (i.e. a scalar string). The file will |
| 74 |
be read and parsed. |
| 75 |
|
| 76 |
Except in the case of Path::Class::File, IO::All and strings, you |
| 77 |
need to tell the `parse` function what parser to use, and what |
| 78 |
base URI to use. |
| 79 |
|
| 80 |
* a string |
| 81 |
|
| 82 |
You need to tell the `parse` function what parser to use, and what |
| 83 |
base URI to use. |
| 84 |
|
| 85 |
* a model or store |
| 86 |
|
| 87 |
An existing model or store, which will just be returned as-is. |
| 88 |
|
| 89 |
* undef |
| 90 |
|
| 91 |
Returns an empty model. |
| 92 |
|
| 93 |
The `parser` option can be used to provide a blessed |
| 94 |
RDF::Trine::Parser object to use; the `type` option can be used |
| 95 |
instead to provide a media type hint. The `base` option provides the |
| 96 |
base URI. The `model` option can be used to tell this function to |
| 97 |
parse into an existing model rather than returning a new one. The |
| 98 |
`graph` option may be used to provide a graph URI. |
| 99 |
|
| 100 |
`into` is an alias for `model`; `type`, `using` and `as` are aliases |
| 101 |
for `parser`; `context` is an alias for `graph`. |
| 102 |
|
| 103 |
Examples: |
| 104 |
|
| 105 |
my $model = parse('/tmp/data.ttl', as => 'Turtle'); |
| 106 |
|
| 107 |
my $data = iri('http://example.com/data.nt'); |
| 108 |
my $parser = RDF::Trine::Parser::NTriples->new; |
| 109 |
my $model = model(); |
| 110 |
|
| 111 |
parse($data, using => $parser, into => $model); |
| 112 |
|
| 113 |
`serialize($data, %options)` |
| 114 |
Serializes the data (which can be an RDF::Trine::Model or an |
| 115 |
RDF::Trine::Iterator) and returns it as a string. |
| 116 |
|
| 117 |
The `serializer` option can be used to provide a blessed |
| 118 |
RDF::Trine::Serializer object to use; the `type` option can be used |
| 119 |
instead to provide a type hint. The `output` option can be used to |
| 120 |
provide a filehandle, IO::All, Path::Class::File or file name to write |
| 121 |
to instead of returning the results as a string. |
| 122 |
|
| 123 |
`to` and `file` are aliases for `output`; `type`, `using` and `as` are |
| 124 |
aliases for `serializer`. |
| 125 |
|
| 126 |
Examples: |
| 127 |
|
| 128 |
print serialize($model, as => 'Turtle'); |
| 129 |
|
| 130 |
my $file = Path::Class::File->new('/tmp/data.nt'); |
| 131 |
serialize($iterator, to => $file, as => 'NTriples'); |
| 132 |
|
| 133 |
Array References |
| 134 |
In addition to the above interface, each function supports being called |
| 135 |
with a single arrayref argument. In those cases, the arrayref is |
| 136 |
dereferenced into an array, and treated as a list of arguments. That is, |
| 137 |
the following are equivalent: |
| 138 |
|
| 139 |
foo($bar, $baz); |
| 140 |
foo([$bar, $baz]); |
| 141 |
|
| 142 |
This is handy if you're writing a module of your own and wish to accept |
| 143 |
some RDF data: |
| 144 |
|
| 145 |
sub my_method { |
| 146 |
my ($self, $rdf, $foo) = @_; |
| 147 |
$rdf = parse($rdf); |
| 148 |
|
| 149 |
.... |
| 150 |
} |
| 151 |
|
| 152 |
Your method can now be called like this: |
| 153 |
|
| 154 |
$object->my_method($model, 'foo'); |
| 155 |
|
| 156 |
$object->my_method($url, 'foo'); |
| 157 |
|
| 158 |
$object->my_method( |
| 159 |
[ $filehandle, as => 'Turtle', base => $uri ], |
| 160 |
'foo', |
| 161 |
); |
| 162 |
|
| 163 |
Export |
| 164 |
By default, nothing is exported. You need to request things: |
| 165 |
|
| 166 |
use RDF::TrineX::Functions qw< iri literal blank statement model >; |
| 167 |
|
| 168 |
Thanks to Sub::Exporter, you can rename functions: |
| 169 |
|
| 170 |
use RDF::TrineX::Functions |
| 171 |
qw< literal statement model >, |
| 172 |
blank => { -as => 'bnode' }, |
| 173 |
iri => { -as => 'resource' }; |
| 174 |
|
| 175 |
If you want to export everything, you can do: |
| 176 |
|
| 177 |
use RDF::TrineX::Functions -all; |
| 178 |
|
| 179 |
To export just the functions which generate RDF::Trine::Node objects: |
| 180 |
|
| 181 |
use RDF::TrineX::Functions -nodes; |
| 182 |
|
| 183 |
Or maybe even: |
| 184 |
|
| 185 |
use RDF::TrineX::Functions -nodes => { -suffix => '_node' }; |
| 186 |
|
| 187 |
If you want to export something roughly compatible with the old |
| 188 |
RDF::TrineShortcuts, then there's: |
| 189 |
|
| 190 |
use RDF::TrineX::Functions -shortcuts; |
| 191 |
|
| 192 |
When exporting the `serialize` function you may set a default format: |
| 193 |
|
| 194 |
use RDF::TrineX::Functions |
| 195 |
serialize => { -type => 'NTriples' }; |
| 196 |
|
| 197 |
This will be used when `serialize` is called with no explicit type given. |
| 198 |
|
| 199 |
Pseudo-OO interface |
| 200 |
`new` |
| 201 |
This acts as a constructor, returning a new RDF::TrineX::Functions |
| 202 |
object. |
| 203 |
|
| 204 |
All the normal functions can be called as methods: |
| 205 |
|
| 206 |
my $R = RDF::TrineX::Functions->new; |
| 207 |
my $model = $R->model; |
| 208 |
|
| 209 |
There's no real advantage to using this module as an object, but it can |
| 210 |
help you avoid namespace pollution. |
| 211 |
|
| 212 |
BUGS |
| 213 |
Please report any bugs to |
| 214 |
<http://rt.cpan.org/Dist/Display.html?Queue=RDF-TrineX-Functions>. |
| 215 |
|
| 216 |
SEE ALSO |
| 217 |
RDF::Trine, RDF::QueryX::Lazy, RDF::NS. |
| 218 |
|
| 219 |
AUTHOR |
| 220 |
Toby Inkster <[email protected]>. |
| 221 |
|
| 222 |
COPYRIGHT AND LICENCE |
| 223 |
This software is copyright (c) 2012 by Toby Inkster. |
| 224 |
|
| 225 |
This is free software; you can redistribute it and/or modify it under the |
| 226 |
same terms as the Perl 5 programming language system itself. |
| 227 |
|
| 228 |
DISCLAIMER OF WARRANTIES |
| 229 |
THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED |
| 230 |
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF |
| 231 |
MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 232 |
|