RDF::NS(3pm)
π 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 π
π COPYRIGHT AND LICENSE π
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
- debian trixie: librdf-ns-perl 20230619-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: librdf-ns-perl 20230619-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu questing: librdf-ns-perl 20230619-1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms