RDF::NS(3pm)

πŸ“¦
this version Β· 6 versions
, in debian trixie, ubuntu noble, ubuntu questing

πŸŽ€ RDF::NS πŸŽ€

okay so bestie, welcome to RDF::NS β€” a Perl module (it's in the User Contributed Perl Documentation section of your man pages, iykyk) that hands you all the popular RDF namespace prefixes so you never have to hardcode them again. let's get into it πŸ’–


✨ NAME ✨

RDF::NS β€” just use popular RDF namespace prefixes from prefix.cc. that's it. that's the whole vibe. πŸ’…


🌸 SYNOPSIS 🌸

okay so here's the whole thing in action, no notes, just copy it:

use RDF::NS '20230619';              # check at compile time
my $ns = RDF::NS->new('20230619');   # 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/"

# load your own mapping from a file
$ns = RDF::NS->new("mapping.txt");

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

# instances of RDF::NS 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 keys %$ns) . "prefixes\n";
$ns->COUNT;              # also returns the number of prefixes

literally so much going on and every bit of it slays ✨


πŸ¦‹ DESCRIPTION πŸ¦‹

okay so here's the tea: hardcoding URI namespaces and prefixes for your RDF apps? neither fun nor maintainable. like, at all. and honestly at the end of the day we're all out here using more or less the same prefix definitions, the ones collected over at <http://prefix.cc>. so this module just... includes all of them. as they were defined at specific snapshots in time. 🫢

and those snapshots? they correspond to version numbers of this module. so by selecting a particular version you make sure that changes at prefix.cc won't affect your programs. iconic, honestly. stable queen behavior. πŸ‘‘

also, the command line client rdfns gets installed automatically with this module, no extra steps:

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

ngl this module does not require RDF::Trine β€” but it is recommended nevertheless (at least version 0.140). so like, it's giving optional but iconic. if you'd rather have RDF::NS return instances of RDF::Trine::Node::Resource instead of plain strings, use RDF::NS::Trine. and RDF::NS::URIS is a similar module that returns instances of URI. pick your fighter πŸ’…

also the code repository of this module contains an update script <https://github.com/nichtich/RDF-NS/blob/master/update.pl> that downloads the current prefix-namespace mappings from <http://prefix.cc>. so you can keep it fresh πŸ“


πŸ’– GENERAL METHODS πŸ’–

in most cases you only need the following lowercase methods. they're the main characters fr fr.

🌈 new ( [ $file_or_date ] [ %options ] )

creates a new namespace mapping from a selected file, date, or hash reference. the special string "any" or the value 1 can be used to get the newest mapping β€” but bestie, you should better select a specific version, because mappings can change, and that would violate backwards compatibility 😭. supported options include "warn" to enable warnings and "at" to specify a date.

✨ prefix

returns the namespace for prefix if namespace prefix is defined. for instance $ns->foaf returns http://xmlns.com/foaf/0.1/. simple. clean. slay.

🌸 prefix_name

returns the namespace plus local name, if namespace prefix is defined. for instance $ns->foaf_Person returns http://xmlns.com/foaf/0.1/Person. two for one, obsessed.

πŸ¦‹ 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. and if you pass an URI wrapped in < and >, it will not be expanded but returned as given β€” it just comes back exactly how it left, no glow-up required πŸ’œ


πŸŽ€ SERIALIZATION METHODS πŸŽ€

πŸ’… 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).


🧚 LOOKUP METHODS 🧚

πŸ’€ PREFIX ( $uri )

get a prefix of a namespace URI, if it is defined. heads up though: this method does a reverse lookup, which is less performant than the other direction 😭. if multiple prefixes are defined, the first in sorted order is returned. and if you need to call this method frequently and with deterministic response, you'd better create a reverse hash (method REVERSE).

🌈 PREFIXES ( $uri )

get all known prefixes of a namespace URI, in sorted order. the whole squad, alphabetized 🫢

πŸ’– REVERSE

calling $ns->REVERSE is equal to RDF::SN->new($ns). see RDF::SN for details.

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


πŸŽ€ INTERNAL METHODS πŸŽ€

πŸ”§ SET ( $prefix => $namespaces [, $warn ] )

set or add a namespace mapping. errors are ignored unless enabled as warnings with the third argument. returns true if the mapping was successfully added.

πŸ› οΈ 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, URI::NamespaceMap, RDF::Prefixes, RDF::Simple::NS, RDF::RDFa::Parser::Profile::PrefixCC, Class::RDF::NS, XML::Namespace, XML::CommonNS etc. the whole extended universe, honestly πŸ’œ


this software is copyright (c) 2013- 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.


and that's the whole page bestie, no cap 🫢 you got this, go expand some prefixes πŸ’…

this was RDF::NS, documented for perl v5.38.2, dated 2024-01-21 β€” mwah, periodt πŸ’–

πŸ“¦ where this page comes from