An open API service indexing awesome lists of open source software.

https://github.com/swh/perl-sparql-client-library

A simple Perl library for accessing SPARQL endpoints.
https://github.com/swh/perl-sparql-client-library

Last synced: 3 months ago
JSON representation

A simple Perl library for accessing SPARQL endpoints.

Awesome Lists containing this project

README

        

About

This module is an implementation of the SPARQL protocol and result format
with minimal dependencies. Specifically it should be possible to run this
module using only packages provided by your distribution, making it suitable
for corporate environments.

The dependencies are:
LWP::UserAgent
LWP::ConnCache
URI::Escape
XML::Simple

This module will also use Keepalive HTTP Connections where possible.

Usage

use sparql;

my $sparql = sparql->new();

my $res = $sparql->query('http://dbpedia.org/sparql',
'SELECT * WHERE { ?s ?p ?o } LIMIT 10');

for my $row (@{$res}) {
for my $col (keys %{$row}) {
print('?'.$col.' = '.$row->{$col}."\n");
}
print("\n");
}

Methods

new()

Returns a SPARQL client object.

query(endpoint, query string)

Takes and endpoint URI, and a query, and returns a reference to an array of
hashes, for SELECT, and a string for CONSTRUCT etc. Each element in the
array (a hash) is a solution, the keys of the hash are the variable name
(without the ? or $), and the value is a Turtle literal representing the result
value.

update(endpoint, update string)

Takes an endpoint URI, and a query, and returns a string of the returned message.

put(endpoint, graph uri, mime type, RDF text)

Takes an endpoint URI, and graph to write to, a MIME type and some RDF
data, and writes the data into the graph at the endpoint.

Example: $sparql->put('http://host.example/data', 'http://mygraph.example/data.rdf',
'text/turtle', "

.\n");

Returns whatever text the endpoint returns as a result.

chomp(turtle literal)

Removes any Turtle-style gubbins from around a returned value, e.g. '"foo"' -> 'foo',
'' -> 'http://example.com/'.

print(result)

Formats query a result object and prints it to STDOUT.

Licence

The sparql.pm module itsself is licenced under the Lesser GPL, version 3+,
and the example code is 2-clause BSD.