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

NAME

RDF::NS - Just use popular RDF namespace prefixes from prefix.cc

VERSION

version 20120124

SYNOPSIS

  use RDF::NS '20120124';              # check at compile time
  my $ns = RDF::NS->new('20120124');   # check at runtime

  $ns->foaf;               # http://xmlns.com/foaf/0.1/
  $ns->foaf_Person;        # http://xmlns.com/foaf/0.1/Person
  $ns->foaf('Person');     # http://xmlns.com/foaf/0.1/Person
  $ns->URI('foaf:Person'); # http://xmlns.com/foaf/0.1/Person

  use RDF::NS;             # get rid if typing '$' by defining a constant
  use constant NS => RDF::NS->new('20111208');
  NS->foaf_Person;         # http://xmlns.com/foaf/0.1/Person

  $ns->SPAQRL('foaf');     # PREFIX foaf: <http://xmlns.com/foaf/0.1/>
  $ns->TTL('foaf');        # @prefix foaf: <http://xmlns.com/foaf/0.1/> .
  $ns->XMLNS('foaf');      # xmlns:foaf="http://xmlns.com/foaf/0.1/"

  # To get RDF::Trine::Node::Resource instead of strings
  use RDF::NS::Trine;
  $ns = RDF::NS::Trine->new('20120124');
  $ns->foaf_Person;        # iri('http://xmlns.com/foaf/0.1/Person')

  # load your own mapping
  $ns = RDF::NS::LOAD("mapping.txt");

  # select particular mappings
  %map = $ns->SELECT('rdf,dc,foaf');
  $uri = $ns->SELECT('foo|bar|doz'); # returns first existing namespace

  # instances are just blessed hash references
  $ns->{'foaf'};           # http://xmlns.com/foaf/0.1/
  bless { foaf => 'http://xmlns.com/foaf/0.1/' }, 'RDF::NS';
  print (scalar %$ns) . "prefixes\n";

DESCRIPTION

Hardcoding URI namespaces and prefixes for RDF applications is neither fun nor maintainable. In the end we all use more or less the same prefix definitions, as collected at http://prefix.cc. This module includes all these prefixes as defined at specific snapshots in time. These snapshots correspond to version numbers of this module. By selecting particular versions, you make sure that changes at prefix.cc won't affect your scripts.

The command line client rdfns is installed automatically with this module:

  $ rdfns -ttl rdf,foaf
  @prefix foaf: <http://xmlns.com/foaf/0.1/> .
  @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

This module does not require RDF::Trine, which is recommended nevertheless. If you prefer RDF::NS to return instances of RDF::Trine::Node::Resource instead of plain strings, use RDF::NS::Trine.

The code repository of this module also contains an update script to download the current prefix-namespace mappings from http://prefix.cc.

METHODS

new ( $version [, %options ] )

Create a new namespace mapping with a selected version (mandatory). The special version string "any" can be used to get the newest mapping - actually this is $RDF::NS::VERSION, but you should better select a specific version, as mappings can change, violating backwards compatibility. Supported options include warn to enable warnings.

LOAD ( $file [, %options ] )

Load namespace mappings from a particular tab-separated file. See NEW for supported options.

URI ( $short | "<$URI>" )

Expand a prefixed URI, such as foaf:Person or foaf_Person. Alternatively you can expand prefixed URIs with method calls, such as $ns->foaf_Person. If you pass an URI wrapped in < and >, it will not be expanded but returned as given.

TTL ( prefix[es] )

Returns a Turtle/Notation3 @prefix definition or a list of such definitions in list context. Prefixes can be passed as single arguments or separated by commas, vertical bars, and spaces.

SPARQL ( prefix[es] )

Returns a SPARQL PREFIX definition or a list of such definitions in list context. Prefixes can be passed as single arguments or separated by commas, vertical bars, and spaces.

XMLNS ( prefix[es] )

Returns an XML namespace declaration or a list of such declarations in list context. Prefixes can be passed as single arguments or separated by commas, vertical bars, and spaces.

TXT ( prefix[es] )

Returns a list of tabular-separated prefix-namespace-mappings.

BEACON ( prefix[es] )

Returns a list of BEACON format prefix definitions (not including prefixes).

SELECT ( prefix[es] )

In list context, returns a sorted list of prefix-namespace pairs, which can be used to assign to a hash. In scalar context, returns the namespace of the first prefix that was found. Prefixes can be passed as single arguments or separated by commas, vertical bars, and spaces.

MAP ( $code [, prefix[es] ] )

Internally used to map particular or all prefixes. Prefixes can be selected as single arguments or separated by commas, vertical bars, and spaces. In scalar context, $_ is set to the first existing prefix (if found) and $code is called. In list context, found prefixes are sorted at mapped with $code.

GET ( $uri )

This method is used internally to create URIs as return value of the URI method and all lowercase shortcut methods, such as foaf_Person. By default it just returns $uri unmodified.

SEE ALSO

There are several other CPAN modules to deal with IRI namespaces, for instance RDF::Trine::Namespace, RDF::Trine::NamespaceMap, RDF::Prefixes, RDF::Simple::NS, RDF::RDFa::Parser::Profile::PrefixCC, Class::RDF::NS, XML::Namespace, XML::CommonNS etc.

AUTHOR

Jakob Voss

COPYRIGHT AND LICENSE

This software is copyright (c) 2012 by Jakob Voss.

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