File Coverage

blib/lib/RDF/SN.pm
Criterion Covered Total %
statement 39 39 100.0
branch 16 18 88.8
condition 17 21 80.9
subroutine 8 8 100.0
pod 2 3 66.6
total 82 89 92.1


line stmt bran cond sub pod time code
1             package RDF::SN;
2 6     6   74 use v5.10;
  6         22  
3 6     6   31 use strict;
  6         11  
  6         126  
4 6     6   28 use warnings;
  6         13  
  6         244  
5              
6             our $VERSION = '20181102';
7              
8 6     6   32 use RDF::NS;
  6         13  
  6         157  
9 6     6   30 use Scalar::Util qw(blessed);
  6         10  
  6         3615  
10              
11             sub new {
12 22     22 1 59 my ($class, $ns) = @_;
13              
14 22 100       107 unless (blessed $ns) {
15 1 50       10 $ns = $ns ? RDF::NS->new($ns) : RDF::NS->new;
16             }
17              
18 22         78 my $self = bless { }, $class;
19            
20 22         99 while ( my ($prefix, $namespace) = each %$ns ) {
21 28763         39272 my $has = $self->{$namespace};
22 28763 100 100     51720 if (!$has || (length($has) > length($prefix))
      100        
      100        
23             || (length($has) == length($prefix) and $has ge $prefix)
24             ) {
25 27415         73954 $self->{$namespace} = $prefix;
26             }
27             }
28              
29 22         493 $self;
30             }
31              
32             sub qname {
33 21     21 1 47 my ($self, $uri) = @_;
34              
35 21 100       57 if ($self->{$uri}) {
36 16 100       158 return wantarray ? ($self->{$uri}, '') : $self->{$uri}.':';
37             }
38              
39             # regexpes copied from RDF::Trine::Node::Resource
40 5   66     31 our $r_PN_CHARS_BASE ||= qr/([A-Z]|[a-z]|[\x{00C0}-\x{00D6}]|[\x{00D8}-\x{00F6}]|[\x{00F8}-\x{02FF}]|[\x{0370}-\x{037D}]|[\x{037F}-\x{1FFF}]|[\x{200C}-\x{200D}]|[\x{2070}-\x{218F}]|[\x{2C00}-\x{2FEF}]|[\x{3001}-\x{D7FF}]|[\x{F900}-\x{FDCF}]|[\x{FDF0}-\x{FFFD}]|[\x{10000}-\x{EFFFF}])/;
41 5   66     254 our $r_PN_CHARS_U ||= qr/(_|${r_PN_CHARS_BASE})/;
42 5   66     160 our $r_PN_CHARS ||= qr/${r_PN_CHARS_U}|-|[0-9]|\x{00B7}|[\x{0300}-\x{036F}]|[\x{203F}-\x{2040}]/;
43 5   66     378 our $r_PN_LOCAL ||= qr/((${r_PN_CHARS_U})((${r_PN_CHARS}|[.])*${r_PN_CHARS})?)/;
44              
45 5 100       1203 if ($uri =~ m/${r_PN_LOCAL}$/) {
46 3         14 my $ln = $1;
47 3         11 my $ns = substr($uri, 0, length($uri)-length($ln));
48 3 50       13 if ($self->{$ns}) {
49 3 100       34 return(wantarray ? ($self->{$ns},$ln) : $self->{$ns}.':'.$ln);
50             }
51             }
52              
53 2         15 return;
54             }
55              
56             sub qname_ {
57 2 100   2 0 6 if(wantarray) {
58 1         4 return $_[0]->qname($_[1]);
59             } else {
60 1         4 return join '_', $_[0]->qname($_[1]);
61             }
62             }
63              
64             1;
65             __END__