RDF::aREF::Encoder(3) encode RDF to another RDF Encoding Form


use RDF::aREF::Encoder;
my $encoder = RDF::aREF::Encoder->new;

# encode parts of aREF
my $qname = $encoder->qname('http://schema.org/Review'); # 'schema_Review'
my $predicate = $encoder->predicate({
type => 'uri',
value => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type'
}); # 'a'
my $object = $encoder->object({
type => 'literal',
value => 'hello, world!',
lang => 'en'
}); # 'hello, world!@en'
# method also accepts RDF::Trine::Node instances
my $object = $encoder->object( RDF::Trine::Resource->new($iri) );
# encode RDF graphs (see also function 'encode_aref' in RDF::aREF)
use RDF::Trine::Parser;
my $aref = { };
RDF::Trine::Parser->parse_file ( $base_uri, $fh, sub {
my $s = shift;
$encoder->triple( $s->subject, $s->predicate, $s->object, $aref );
} );


This module provides methods to encode RDF data in another RDF Encoding Form (aREF). As aREF was designed to facilitate creation of RDF data, it may be easier to create aREF ``by hand'' instead of using this module!



A default namespace map, given as version string of module RDF::NS for stable qNames or as instance of RDF::NS. The most recent installed version of RDF::NS is used by default. The value 0 can be used to only use required namespace mappings (rdf, rdfs, owl and xsd).


By default RDF graphs with common subject are encoded as aREF predicate map:

      _id => $subject, $predicate => $object

Enable this option to always encode as aREF subject map:

       $subject => { $predicate => $object }


Note that no syntax checking is applied, e.g. whether a given URI is a valid URI or whether a given language is a valid language tag!

qname( $uri )

Abbreviate an URI as qName or return "undef". For instance "http://purl.org/dc/terms/title" is abbreviated to ""dct_title"".

uri( $uri )

Abbreviate an URI or as qName or enclose it in angular brackets.

literal( $value, $language_tag, $datatype_uri )

Encode a literal RDF node by either appending ""@"`` and an optional language tag, or ''"^"" and an datatype URI.

bnode( $identifier )

Encode a blank node by prepending ""_:"" to its identifier.

subject( $subject )

predicate( $predicate )

object( $object )

Encode an RDF subject, predicate, or object respectively. The argument must either be given as hash reference, as defined in RDF/JSON <http://www.w3.org/TR/rdf-json/> format (see also method "as_hashref" of RDF::Trine::Model), or as array reference as internally used by RDF::Trine.

A hash reference is expected to have the following fields:

one of "uri", "literal" or "bnode" (required)
the URI of the object, its lexical value or a blank node label depending on whether the object is a uri, literal or bnode
the language of a literal value (optional but if supplied it must not be empty)
the datatype URI of the literal value (optional)

An array reference is expected to consists of

  • three elements (value, language tag, and datatype uri) for literal nodes,
  • two elements ""URI"" and the URI for URI nodes,
  • two elements ""BLANK"" and the blank node identifier for blank nodes.

triple( $subject, $predicate, $object, [, $aref ] )

Encode an RDF triple, its elements given as explained for method "subject", "predicate", and "object". If an aREF data structure is given as fourth argument, the triple is added to this structure, possibly changing an aREF predicate map to an aRef subject map. Returns "undef" on failure.

add_hashref( $aref, $rdf )

Add RDF given in RDF/JSON <http://www.w3.org/TR/rdf-json/> format (as returned by method "as_hashref" in RDF::Trine::Model).

add_iterator( $aref, $iterator )

Add a RDF::Trine::Iterator to an aREF subject map.