RDF::Query updates

January 7th, 2008 10:56 AM

I mentioned in the last post that I’ve made a change in RDF::Query that will break backwards compatibility. The change involves the values (bindings) that are returned from a SELECT query. Previously, a query might look like this:

my $query = new RDF::Query ( 'SELECT ?s ?o WHERE { ?s ?p ?o }' );
my $iterator = $query->execute( $model );
while (my $row = $iterator->()) {
  my ($s, $o) = @$row;
  ...
}

The iterator was only guaranteed to be a CODE reference, and the values it returned were ARRAY references containing the bindings. This code would now look like this:

my $query = new RDF::Query ( 'SELECT ?s ?o WHERE { ?s ?p ?o }' );
my $iterator = $query->execute( $model );
while (my $row = $iterator->next()) {
  my $s = $row->{s};
  my $o = $row->{o};
  ...
}

The iterator is now a RDF::Trine::Iterator::Bindings object, and so is accessed using the next method. The values it returns are always HASH references, exposing the binding values by name. I’m still debating whether these bindings should be objects instead of HASH references, but I think this new interface (while slightly slower) is much more clear, especially for queries using the SELECT * form where variables aren’t explicitly named or ordered.

I’ll be working toward a 2.0 release for RDF::Query in the upcoming weeks (or possibly a month or two) that will include these changes. I’ll try to release a few development builds (as versions 2.00_xx) so that these changes can be seen and used before an official release. Hopefully this will be enough of a warning of these changes that anyone using RDF::Query won’t be caught totally offguard (like the recent JSON debacle which affected my development of RDF::Query).