[SCM] librdf-ns-perl Debian packaging branch, master, updated. debian/20130208-1-22-g47de138

Jakob Voss voss at gbv.de
Fri Apr 19 13:28:43 UTC 2013


The following commit has been merged in the master branch:
commit d2dc163fe7a65bb2a731c6ad93c7ad0bc29bac27
Author: Jakob Voss <voss at gbv.de>
Date:   Mon Aug 27 08:49:46 2012 +0200

    method REVERSE

diff --git a/lib/RDF/NS.pm b/lib/RDF/NS.pm
index fbf8abb..02c9f44 100644
--- a/lib/RDF/NS.pm
+++ b/lib/RDF/NS.pm
@@ -72,6 +72,17 @@ sub PREFIXES {
 	return @prefixes;
 }
 
+sub REVERSE {
+    my $self = shift;
+    my $lookup = { };
+	while ( my ($prefix, $namespace) = each %$self ) {
+        my $has = $lookup->{$namespace};
+        $lookup->{$namespace} = $prefix unless
+            $has and length($has) < length($prefix);
+	}
+	return $lookup;
+}
+
 sub TTL {
     my $self = shift;
     $self->MAP( sub { "\@prefix $_: <".$self->{$_}."> ." } , @_ );
@@ -255,12 +266,18 @@ Returns a list of BEACON format prefix definitions (not including prefixes).
 
 Get a prefix of a namespace URI, if it is defined. This method does a reverse
 lookup which is less performant than the other direction. If multiple prefixes
-are defined, it is not determinstic which one is returned.
+are defined, it is not determinstic which one is returned. If you need to call
+this method frequently, better create a reverse hash (method REVERSE).
 
 =method PREFIXES ( $uri )
 
 Get all known prefixes of a namespace URI.
 
+=method REVERSE
+
+Create a lookup hash from namespace URIs to prefixes. If multiple prefixes
+exist, the shortes will be used.
+
 =method SELECT ( prefix[es] )
 
 In list context, returns a sorted list of prefix-namespace pairs, which
diff --git a/t/prefix.t b/t/prefix.t
index f7d3bb8..d853c96 100644
--- a/t/prefix.t
+++ b/t/prefix.t
@@ -5,13 +5,19 @@ use Test::More;
 use RDF::NS;
 
 my $rdfs = 'http://www.w3.org/2000/01/rdf-schema#';
+my $dc   = 'http://purl.org/dc/elements/1.1/';
 
 my $ns = RDF::NS->new('20111028');
 
 is $ns->PREFIX('http://www.w3.org/1999/02/22-rdf-syntax-ns#'), 'rdf', 'PREFIX';
 
-my @nslist = sort $ns->PREFIXES('http://purl.org/dc/elements/1.1/');
+my @nslist = sort $ns->PREFIXES($dc);
 
 is_deeply \@nslist, [qw(dc dc11)], 'PREFIXES';
 
+my $rev = $ns->REVERSE;
+
+is $rev->{$rdfs}, 'rdfs', 'reverse';
+is $rev->{$dc}, 'dc', 'reverse';
+
 done_testing;

-- 
librdf-ns-perl Debian packaging



More information about the Pkg-perl-cvs-commits mailing list