Author image Jakob Voß
and 1 contributors


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


version 0.004


  use Turtle::Writer;

  say turtle_statement( 
      "a" => "<>",
      "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


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.


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>".


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 = '';
            $uri => {
                a          => '',
                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);


Jakob Voß


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.