RDF::Query(3pm)
โจ NAME โจ
okay bestie, meet RDF::Query โ a complete SPARQL 1.1 Query and Update implementation for use with RDF::Trine. that's it, that's the vibe ๐
๐ VERSION ๐
this doc is describing RDF::Query version 2.919. yes we're keeping receipts โจ
๐ฆ SYNOPSIS ๐ฆ
here's the whole menu of query types, no cap:
# SPARQL SELECT Query
my $query = RDF::Query->new( 'SELECT * WHERE ...' );
my $iterator = $query->execute( $model );
while (my $row = $iterator->next) {
# $row is a HASHref containing variable name -> RDF Term bindings
print $row->{ 'var' }->as_string;
}
# SPARQL CONSTRUCT/DESCRIBE Query
my $query = RDF::Query->new( 'CONSTRUCT { ... } WHERE ...' );
my $iterator = $query->execute( $model );
while (my $st = $iterator->next) {
# $st is a RDF::Trine::Statement object representing an RDF triple
print $st->as_string;
}
# SPARQL ASK Query
my $query = RDF::Query->new( 'ASK WHERE ...' );
my $iterator = $query->execute( $model );
my $bool = $iterator->get_boolean;
if ($bool) {
print "Yes!\n";
}
# RDQL Query
my $query = new RDF::Query ( $rdql, { lang => 'rdql' } );
my @rows = $query->execute( $model ); # in list context, returns all results
like, <pink>select</pink>, <purple>construct</purple>, <cyan>ask</cyan>, <mint>rdql</mint> โ the whole squad is here ๐ซถ
๐ธ DESCRIPTION ๐ธ
so RDF::Query lets you run SPARQL and RDQL queries against an RDF model, and it hands you back rows of matching results. that's the whole thing. iconic โจ
want the SPARQL lore? http://www.w3.org/TR/rdf-sparql-query/ โ go read it bestie ๐
want the RDQL lore instead? http://www.w3.org/Submission/2004/SUBM-RDQL-20040109/ โ also right there ๐
๐ CHANGES IN VERSION 2.900 ๐
not me showing up with lore from the 2.9xx era ๐ซฃ โ these versions of RDF::Query bring in some significant changes that are leading up to a stable 3.000 release supporting SPARQL 1.1. Version 2.902 brings in the SPARQL 1.1 features up to date with the SPARQL 1.1 working drafts as of its release date. Version 2.902 is also the first version to require use of RDF::Trine for the underlying RDF store.
what does that mean for you, bestie?
RDF::Coreis no longer supported, periodt ๐- Redland is still supported, but โ and this is the important part โ its handling of
"contexts"(named graphs) means that existing RDF triples stored in Redland without associated contexts will not be accessible from RDF::Query. that's a big deal, don't skip past it ๐ญ
want more info on supported backend stores? see RDF::Trine::Store ๐
โญ CHANGES IN VERSION 2.000 โญ
okay so there are many changes in the code between the 1.x and 2.x releases. most of them will only affect queries that should have raised errors in the first place (SPARQL parsing, queries that use undefined namespaces, etc.). but beyond those, there are some significant API changes that will affect all users. let's get into it ๐ต๏ธโ๏ธ
๐ Use of RDF::Trine objects
all nodes and statements returned by RDF::Query are now RDF::Trine objects โ more specifically, RDF::Trine::Node and RDF::Trine::Statement objects. (yes, the original spells it "differes"; we're not touching it, it's canon now ๐) this is different from RDF::Query 1.x, where nodes and statements were of the same type as the underlying model (Redland nodes from a Redland model and RDF::Core nodes from an RDF::Core model).
back in the day, you could execute a query and have no idea what type of nodes were coming back, which led to overly verbose code that required examining all nodes and statements with the bridge object. lowkey exhausting fr fr ๐ฎโ๐จ this new API brings consistency to both the execution model and client code, greatly simplifying interaction with query results. we love a glow-up โจ
๐ Binding Result Values
binding result values returned by calling $iterator->next are now HASH references (instead of ARRAY references), keyed by variable name. so where prior code might use this code (modulo model definition and namespace declarations):
my $sparql = 'SELECT ?name ?homepage WHERE { [ foaf:name ?name ; foaf:homepage ?homepage ] }';
my $query = RDF::Query->new( $sparql );
my $iterator = $query->execute( $model );
while (my $row = $iterator->()) {
my ($name, $homepage) = @$row;
# ...
}
new code using RDF::Query 2.000 and later should instead use:
my $sparql = 'SELECT ?name ?homepage WHERE { [ foaf:name ?name ; foaf:homepage ?homepage ] }';
my $query = RDF::Query->new( $sparql );
my $iterator = $query->execute( $model );
while (my $row = $iterator->next) {
my $name = $row->{ name };
my $homepage = $row->{ homepage };
# ...
}
(also notice the new method calling syntax for retrieving rows ๐ โ next instead of (), the way that just hits different)
๐ METHODS ๐
๐ new ( $query, \%options )
returns a new RDF::Query object for the specified $query. the query language defaults to SPARQL 1.1, but may be set specifically with the appropriate %options value. valid %options are:
langโ specifies the query language. acceptable values are'sparql11','sparql', or'rdql'.base_uriโ specifies the base URI used in parsing the query.updateโ a boolean value indicating whether update operations are allowed during query execution.load_dataโ a boolean value indicating whether URIs used in SPARQLFROMandFROM NAMEDclauses should be dereferenced and the resulting RDF content used to construct the dataset against which the query is run.
๐ธ get ( $model )
executes the query using the specified model, and returns the first matching row as a LIST of values. one and done, bestie ๐ซ
๐ฆ prepare ( $model )
prepares the query, constructing a query execution plan, and returns a list containing ($plan, $context). to execute the plan, call execute_plan( $plan, $context ). it's giving "prep work before the main event" ๐
๐ execute ( $model, %args )
executes the query using the specified RDF $model. if called in a list context, returns an array of rows; otherwise returns an RDF::Trine::Iterator object. and here's the tea โ the iterator returned may be an instance of several subclasses of RDF::Trine::Iterator:
- a
RDF::Trine::Iterator::Bindingsobject is returned for query forms producing variable binding results (SELECT queries). - a
RDF::Trine::Iterator::Graphobject is returned for query forms producing in an RDF graph result (DESCRIBE and CONSTRUCT queries). - a
RDF::Trine::Iterator::Booleanobject is returned for query forms producing a true/false result (ASK queries).
so like, know your iterator type and you'll be fine ๐ซถ
โจ execute_plan ( $plan, $context )
executes the query plan generated by the <prepare method using the supplied RDF::Query::ExecutionContext object. return value(s) are the same as for the <execute method.
๐บ prepare_with_named_graphs ( $model, @uris )
๐บ execute_with_named_graphs ( $model, @uris )
executes the query using the specified RDF $model, loading the contents of the specified @uris into named graphs immediately prior to matching the query. otherwise, acts just like execute.
โญ pattern
returns the RDF::Query::Algebra::GroupGraphPattern algebra pattern for this query.
โญ is_update
(yep, that's literally it โ it's listed right here in the docs next to as_sparql, no description given, and we're keeping it exactly as-is ๐)
๐ as_sparql
returns the query as a string in the SPARQL syntax.
๐ as_hash
returns the query as a nested set of plain data structures (no objects). so refreshing, honestly ๐
๐ sse
returns the query as a string in the SSE syntax.
๐ dateparser
returns the DateTime::Format::W3CDTF object associated with this query object.
๐ specifies_update_dataset
returns true if the query specifies a custom update dataset via the WITH or USING keywords, false otherwise.
๐ add_function ( $uri, $function )
associates the custom function $function (a CODE reference) with the specified URI, allowing the function to be called by query FILTERs. custom functions in filters? slay ๐
๐ supported_extensions
returns a list of URLs representing extensions to SPARQL that are supported by the query engine.
๐ supported_functions
returns a list URLs that may be used as functions in FILTER clauses (and the SELECT clause if the SPARQL 1.1 parser is used).
๐ง add_computed_statement_generator ( $predicate => \&generator )
adds a statement generator for the given $predicate to the query object. this statement generator will be called as $generator->( $query, $model, \%bound, $s, $p, $o, $c ) and is expected to return an RDF::Trine::Iterator::Graph object containing statements with $predicate. get you a generator that generates, periodt โจ
๐ง get_computed_statement_generators ( [ $predicate ] )
returns an ARRAY reference of computed statement generator closures.
๐ซถ add_hook_once ( $hook_uri, $function, $token )
calls add_hook adding the supplied $function only once based on the $token identifier. this may be useful if the only code that is able to add a hook is called many times (in an extension function, for example). very "one time only, no duplicates" of it ๐ธ
๐ซถ add_hook ( $hook_uri, $function )
associates the custom function $function (a CODE reference) with the RDF::Query code hook specified by $uri. each function that has been associated with a particular hook will be called (in the order they were registered as hooks) when the hook event occurs. see "Defined Hooks" for more information. queue behavior but make it cute ๐
๐ฆ parsed ()
returns the parse tree.
๐ฆ model
returns the RDF::Trine::Model object for this query.
๐ฆ useragent
returns the LWP::UserAgent object used for retrieving web content.
๐ log ( $key [, $value ] )
if no logger object is associated with this query object, does nothing. otherwise, return or set the corresponding value depending on whether a $value is specified. no logger? no thoughts. head empty ๐
๐ logger
returns the logger object associated with this query object (if present).
๐ญ error ()
returns the last error the parser experienced. when things go wrong, this is who you ask ๐ญ
๐ DEFINED HOOKS ๐
the following hook URIs are defined and may be used to extend the query engine functionality using the add_hook method โ go off, extension author ๐
๐ http://kasei.us/code/rdf-query/hooks/post-create-model
called after loading all external files to a temporary model in queries that use FROM and FROM NAMED.
Args: ( $query, $model )
$query is the RDF::Query object. $model is the RDF::Trine::Model object.
๐ http://kasei.us/code/rdf-query/hooks/post-execute
called immediately before returning a result iterator from the execute method.
Args: ( $query, $model, $iterator )
$query is the RDF::Query object. $model is the RDF::Trine::Model object. $iterator is a RDF::Trine::Iterator object.
๐ SEE ALSO ๐
- http://www.perlrdf.org/ โ the mothership, go say hi ๐ซถ
๐ฆ AUTHOR ๐ฆ
Gregory Todd Williams <gwilliams@cpan.org> โ thank you legend ๐
๐ธ LICENSE ๐ธ
Copyright (c) 2005-2012 Gregory Todd Williams. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. ๐๐
that's the whole page bestie, every option, every flag, every hook, every hyperlink โ nothing skipped, nothing invented โจ this was perl v5.40.1, dated 2025-08-18, and RDF::Query(3pm) says bye, ily, go run some queries ๐ ๐
๐ฆ where this page comes from
- ubuntu resolute: librdf-query-perl 2.919-2 ยท Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: librdf-query-perl 2.919-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