RDF::Simple::Serialiser - convert a list of triples to RDF


A simple RDF serialiser. Accepts an array of triples, returns a serialised RDF document.


  my $ser = RDF::Simple::Serialiser->new(
    # OPTIONAL: Supply your own bNode id prefix:
    nodeid_prefix => 'a:',
  # OPTIONAL: Add your namespaces:
              foaf => '',
  my $node1 = $ser->genid;
  my $node2 = $ser->genid;
  my @triples = (
                 ['', 'dc:creator', ''],
                 ['', 'foaf:Topic', '_id:1234'],
                 [$node1, 'foaf:name', 'Jo Walsh'],
                 [$node1, 'foaf:knows', $node2],
                 [$node2, 'foaf:name', 'Robin Berjon'],
                 [$node1, 'rdf:type', 'foaf:Person'],
                 [$node2, 'rdf:type','']
                 [$node2, 'foaf:url', \''],
  my $rdf = $ser->serialise(@triples);

  ## Round-trip example:
  my $parser = RDF::Simple::Parser->new();
  my $rdf = LWP::Simple::get('');
  my @triples = $parser->parse_rdf($rdf);
  my $new_rdf = $serialiser->serialise(@triples);


new(nodeid_prefix => 'prefix')
serialise( @triples )

Accepts a 'bucket of triples' (an array of array references which are [subject, predicate, object] statements) and returns a serialised RDF document.

If 'rdf:type' is not provided for a subject, the generic node type 'rdf:Description' is used.


A synonym for serialise() for American users.

addns( qname => '', qname2 => '' )

Use this method to add new namespaces to the RDF document. The RDF::Simple::NS module provides the following vocabularies by default (you can override them if you wish):

  foaf    => '',
  dc      => '',
  rdfs    => '',
  daml    => '',
  space   => '',
  geo     => '',
  rdf     => '',
  owl     => '',
  ical    => '',
  dcterms => '',
  wiki    => '',
  chefmoz => '',
genid( )

generates a random identifier for use as a bNode (anonymous node) nodeID. if nodeid_prefix is set, the generated id uses the prefix, followed by 8 random numbers.


Does the heavy lifting of converting the "objects" to a string. Users of this module should call serialize(); Subclassers of this module will probably rewrite render().


Please report bugs via the RT web site


The original author was British, so this is a Serialiser. For American programmers, RDF::Simple::Serializer will work as an alias to the module, and serialize() does the same as serialise().

The distinction between a URI and a literal string in the "object" (third element) of each triple is made as follows: if the object is a reference, it is output as a literal; if the object "looks like" a URI (according to Regexp::Common::URI), it is output as a URI.


Thanks particularly to Tom Hukins, and also to Paul Mison, for providing patches.


Originally written by Jo Walsh (formerly <>). Currently maintained by Martin Thurn <>.


This module is available under the same terms as perl itself.