The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Turtle::Writer - Write RDF/Turtle documents without non-core package dependencies

VERSION

version 0.004

SYNOPSIS

  use Turtle::Writer;

  say turtle_statement( 
    "<$uri>",
      "a" => "<http://purl.org/ontology/bibo/Document>",
      "dc:creator" => { # plain literals are escaped
                "" => [ "Terry Winograd", "Fernando Flores" ]
      },
      "dc:date" => { "xs:gYear" => "1987" }, # typed literal
      "dc:title" =>
          { en => "Understanding Computers and Cognition" },
      "dc:description" => undef,  # will be ignored
  );

DESCRIPTION

Turtle::Writer is a lightweight helper module for Perl programs that write RDF data in Turtle syntax. No non-core packages are required. Before directly writing RDF/Turtle by hand, have a look at this module. Before using this module, have a look at RDF::Trine::Serializer::Turtle which provides a full featured serializer for RDF data in Turtle syntax.

By default this module exports four methods: turtle_statement, turtle_literal, turtle_literal_list, and turtle_uri. This methods may be handy to directly create serialized RDF from other forms of structured data. Literal values are escaped and undef is ignored, among other features.

METHODS

turtle_statement ( $subject, $predicate => $object [, ... ] )

Returns a (set of) RDF statements in Turtle syntax. Subject and predicate parameters must be strings. Object parameters must either be strings or arrays of strings. This function strips undefined values and empty strings, but it does not further check or validate parameter values.

turtle_literal ( $string [ [ lang => ] $lang | [ type => ] $datatype ] )

Returns a literal string escaped in Turtle syntax. You can optionally provide either a language or a full datatype URI (but their values are not validated). Returns the empty string instead of a Turtle value, if $string is undef or the empty string.

turtle_literal_list ( $literal | @array_of_literals | { $language => $literal } )

Returns a list of literal strings in Turtle syntax.

turtle_uri ( $uri )

Returns an URI in Turtle syntax, that is "<$uri>". Returns the empty string, if $uri is undef, but <> if $uri is the empty string. In most cases you better directly write "<$uri>".

SEE ALSO

For a more convenient way to create RDF data, have a look at RDF::aREF. The example given above can be translated to:

    use RDF::aREF;
    use RDF::Trine::Model;
    use RDF::Trine::Serializer;

    my $model = RDF::Trine::Model->new;

    my $uri = 'http://example.org/';
    decode_aref( 
        {
            $uri => {
                a          => 'http://purl.org/ontology/bibo/Document',
                dc_creator => [ 'Terry Winograd', 'Fernando Flores' ],
                dc_date    => '1987^xsd:gYear',
                dc_title   => 'Understanding Computers and Cognition@en',
                dc_description => undef,
            },
        },
        callback => $model
    );

    print RDF::Trine::Serializer->new('Turtle')->serialize_model_to_string($model);

AUTHOR

Jakob Voß

COPYRIGHT AND LICENSE

This software is copyright (c) 2014 by Jakob Voß.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.