File Coverage

blib/lib/URI/urn.pm
Criterion Covered Total %
statement 57 70 81.4
branch 15 28 53.5
condition 2 6 33.3
subroutine 10 11 90.9
pod 1 3 33.3
total 85 118 72.0


line stmt bran cond sub pod time code
1             package URI::urn; # RFC 2141
2              
3 2     2   137469 use strict;
  2         5  
  2         81  
4 2     2   10 use warnings;
  2         4  
  2         180  
5              
6             our $VERSION = '5.34';
7              
8 2     2   549 use parent 'URI';
  2         408  
  2         16  
9              
10 2     2   177 use Carp qw(carp);
  2         6  
  2         604  
11              
12             my %implementor;
13              
14             sub _init {
15 3     3   10 my $class = shift;
16 3         24 my $self = $class->SUPER::_init(@_);
17 3         11 my $nid = $self->nid;
18              
19 3         7 my $impclass = $implementor{$nid};
20 3 100       13 return $impclass->_urn_init($self, $nid) if $impclass;
21              
22 2         5 $impclass = "URI::urn";
23 2 50       13 if ($nid =~ /^[A-Za-z\d][A-Za-z\d\-]*\z/) {
24 2         3 my $id = $nid;
25             # make it a legal perl identifier
26 2         5 $id =~ s/-/_/g;
27 2 50       8 $id = "_$id" if $id =~ /^\d/;
28              
29 2         4 $impclass = "URI::urn::$id";
30 2     2   15 no strict 'refs';
  2         3  
  2         1597  
31 2 50       4 unless (@{"${impclass}::ISA"}) {
  2         19  
32             # Try to load it
33 2         5 my $_old_error = $@;
34 2         188 eval "require $impclass";
35 2 50 66     49 die $@ if $@ && $@ !~ /Can\'t locate.*in \@INC/;
36 2         5 $@ = $_old_error;
37 2 100       25 $impclass = "URI::urn" unless @{"${impclass}::ISA"};
  2         14  
38             }
39             }
40             else {
41 0 0       0 carp("Illegal namespace identifier '$nid' for URN '$self'") if $^W;
42             }
43 2         7 $implementor{$nid} = $impclass;
44 2         21 return $impclass->_urn_init($self, $nid);
45             }
46              
47             sub _urn_init {
48 3     3   10 my($class, $self, $nid) = @_;
49 3         13 bless $self, $class;
50             }
51              
52             sub _nid {
53 4     4   8 my $self = shift;
54 4         25 my $opaque = $self->opaque;
55 4 50       13 if (@_) {
56 0         0 my $v = $opaque;
57 0         0 my $new = shift;
58 0         0 $v =~ s/[^:]*/$new/;
59 0         0 $self->opaque($v);
60             # XXX possible rebless
61             }
62 4         18 $opaque =~ s/:.*//s;
63 4         9 return $opaque;
64             }
65              
66             sub nid { # namespace identifier
67 4     4 0 26 my $self = shift;
68 4         21 my $nid = $self->_nid(@_);
69 4 50       14 $nid = lc($nid) if defined($nid);
70 4         13 return $nid;
71             }
72              
73             sub nss { # namespace specific string
74 5     5 0 7 my $self = shift;
75 5         13 my $opaque = $self->opaque;
76 5 100       11 if (@_) {
77 1         1 my $v = $opaque;
78 1         2 my $new = shift;
79 1 50       2 if (defined $new) {
80 1         7 $v =~ s/(:|\z).*/:$new/;
81             }
82             else {
83 0         0 $v =~ s/:.*//s;
84             }
85 1         2 $self->opaque($v);
86             }
87 5 100       33 return undef unless $opaque =~ s/^[^:]*://;
88 3         8 return $opaque;
89             }
90              
91             sub canonical {
92 0     0 1   my $self = shift;
93 0           my $nid = $self->_nid;
94 0           my $new = $self->SUPER::canonical;
95 0 0 0       return $new if $nid !~ /[A-Z]/ || $nid =~ /%/;
96 0 0         $new = $new->clone if $new == $self;
97 0           $new->nid(lc($nid));
98 0           return $new;
99             }
100              
101             1;