RDF::T...ctions(3pm)
π RDF::TrineX::Functions β some shortcut functions for RDF::Trine's object-oriented interface π
okay bestie, buckle up, because this is RDF::TrineX::Functions, a lil' Perl module from the User Contributed Perl Documentation shelf, and it's literally here to save your fingers from typing out a million ->new calls. no cap. π
β¨ SYNOPSIS β¨
like, here's the whole vibe in one go:
use RDF::TrineX::Functions -all;
my $model = model();
parse('/tmp/mydata.rdf', into => $model);
$model->add_statement(statement(
iri('http://example.com/'),
iri('http://purl.org/dc/terms/title'),
"An Example",
));
print RDF::Trine::Serializer
-> new('Turtle')
-> serialize_model_to_string($model);
see? clean. elegant. we love to see it. π
πΈ DESCRIPTION πΈ
so this module is the replacement for the venerable old RDF::TrineShortcuts. iconic of it. BUT β and this is important, bestie β it is not a drop-in replacement. nope. not even a little.
what it is: fewer features, fewer dependencies, less hackishness, less magic, and fewer places it can go wrong. it's giving glow-up. β¨
it uses Sub::Exporter, which means you can rename exported functions super easily, like:
use RDF::TrineX::Functions
parse => { -as => 'parse_rdf' };
obsessed with that actually. π«Ά
π¦ Functions π¦
iri,literal,blank,variableβ these work as per the similarly named functions exported byRDF::Trineitself. π
but they're wrapped with a very tiny bit of DWIMmery. here's the tea:
- a blessed URI object passed to
irigets handled properly, no drama - a blessed URI object passed to
literalwill default the datatype toxsd:anyURI - a string starting with
_:passed to eitheririorblankwill correctly create a blank node - a string starting with
?passed to eitherblankorvariablewill correctly create a variable - if any of them get passed an existing
RDF::Trine::Node, it just gets passed through untouched, like, it's fine, it's already slaying
other than that? no magic. periodt. β
curieβ likeiribut it passes strings throughRDF::NS::Trine. πstatement(@nodes)β as per the similarly named function exported byRDF::Trineitself.
again, a tiny bit of DWIMmery: blessed URI objects get passed through iri, and unblessed scalars (i.e. strings) are assumed to be literals. cute, right? π
storeβ as per the similarly named function exported byRDF::Trineitself. that's it. that's the whole bullet. πmodelβ returns a newRDF::Trine::Model. it may be passed a store as a parameter. easy.parse($source, %options)β parses the source and returns anRDF::Trine::Model. now, the source can be a whole bunch of things, so let's vibe-check them one by one:- a URI β a string URI, blessed URI object or
RDF::Trine::Node::Resource, which will be retrieved and parsed. π - a file β a filehandle,
Path::Class::File,IO::All,IO::Handleobject, or the name of an existing file (i.e. a scalar string). the file will be read and parsed. π
- a URI β a string URI, blessed URI object or
except in the case ofPath::Class::File,IO::Alland strings, you need to tell theparsefunction what parser to use, and what base URI to use. no guessing, bestie. π
- a string β you need to tell the
parsefunction what parser to use, and what base URI to use. same energy as above. π€ - a model or store β an existing model or store, which will just be returned as-is. lowkey the chillest option. π
- undef β returns an empty model. shrug, it's valid. π
the parser option can be used to provide a blessed RDF::Trine::Parser object to use; the type option can be used instead to provide a media type hint. the base option provides the base URI. the model option can be used to tell this function to parse into an existing model rather than returning a new one. the graph option may be used to provide a graph URI. π
and here's the alias situation: into is an alias for model; type, using and as are aliases for parser; context is an alias for graph. iykyk. π
examples, because we love a demo:
my $model = parse('/tmp/data.ttl', as => 'Turtle');
my $data = iri('http://example.com/data.nt');
my $parser = RDF::Trine::Parser::NTriples->new;
my $model = model();
parse($data, using => $parser, into => $model);
serialize($data, %options)β serializes the data (which can be anRDF::Trine::Modelor anRDF::Trine::Iterator) and returns it as a string. π
the serializer option can be used to provide a blessed RDF::Trine::Serializer object to use; the type option can be used instead to provide a type hint. the output option can be used to provide a filehandle, IO::All, Path::Class::File or file name to write to instead of returning the results as a string. π
alias check: to and file are aliases for output; type, using and as are aliases for serializer. slay. β¨
examples:
print serialize($model, as => 'Turtle');
my $file = Path::Class::File->new('/tmp/data.nt');
serialize($iterator, to => $file, as => 'NTriples');
π§ Array References π§
plot twist: in addition to the above interface, each function supports being called with a single arrayref argument. in those cases, the arrayref is dereferenced into an array, and treated as a list of arguments. so these two are equivalent, fr fr:
foo($bar, $baz);
foo([$bar, $baz]);
this is handy if you're writing a module of your own and wish to accept some RDF data:
sub my_method {
my ($self, $rdf, $foo) = @_;
$rdf = parse($rdf);
....
}
and now your method can be called like this, all these ways:
$object->my_method($model, 'foo');
$object->my_method($url, 'foo');
$object->my_method(
[ $filehandle, as => 'Turtle', base => $uri ],
'foo',
);
versatile queen. π
π Export π
by default, nothing is exported. you need to request things. respectfully. ask for what you want:
use RDF::TrineX::Functions qw< iri literal blank statement model >;
thanks to Sub::Exporter, you can rename functions:
use RDF::TrineX::Functions
qw< literal statement model >,
blank => { -as => 'bnode' },
iri => { -as => 'resource' };
if you want to export everything, you can do:
use RDF::TrineX::Functions -all;
to export just the functions which generate RDF::Trine::Node objects:
use RDF::TrineX::Functions -nodes;
or maybe even:
use RDF::TrineX::Functions -nodes => { -suffix => '_node' };
and if you want to export something roughly compatible with the old RDF::TrineShortcuts, then there's:
use RDF::TrineX::Functions -shortcuts;
when exporting the serialize function you may set a default format:
use RDF::TrineX::Functions
serialize => { -type => 'NTriples' };
this will be used when serialize is called with no explicit type given. convenient much? π
π Pseudo-OO interface π
new
this acts as a constructor, returning a new RDF::TrineX::Functions object. main character energy. β¨
all the normal functions can be called as methods:
my $R = RDF::TrineX::Functions->new;
my $model = $R->model;
ngl there's no real advantage to using this module as an object, but it can help you avoid namespace pollution. so if your namespace is feeling crowded, this is your girl. π«Ά
π BUGS π
please report any bugs to <http://rt.cpan.org/Dist/Display.html?Queue=RDF-TrineX-Functions>.
(not me telling you to file a bug report, but like, do it, the maintainers wanna know π)
π SEE ALSO π
RDF::Trine, RDF::QueryX::Lazy, RDF::NS.
go read those too, bestie, they're all part of the same little universe. πΈ
π AUTHOR π
Toby Inkster <tobyink@cpan.org>.
β COPYRIGHT AND LICENCE β
This software is copyright (c) 2012 by Toby Inkster.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
π DISCLAIMER OF WARRANTIES π
THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
yes bestie, that's the all-caps legal bit. it's important. don't skip it. π
and that's the whole page, periodt. π¦ this was perl v5.34.0, dated 2022-06-17, and you just read the RDF::TrineX::Functions manual. go forth and parse something iconic. π
β¨
π¦ where this page comes from
- debian bookworm: librdf-trinex-functions-perl 0.005-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: librdf-trinex-functions-perl 0.005-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms