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   78544 use strict;
  2         3  
  2         59  
4 2     2   6 use warnings;
  2         3  
  2         145  
5              
6             our $VERSION = '5.35';
7              
8 2     2   343 use parent 'URI';
  2         231  
  2         13  
9              
10 2     2   128 use Carp qw(carp);
  2         4  
  2         427  
11              
12             my %implementor;
13              
14             sub _init {
15 3     3   3 my $class = shift;
16 3         14 my $self = $class->SUPER::_init(@_);
17 3         6 my $nid = $self->nid;
18              
19 3         4 my $impclass = $implementor{$nid};
20 3 100       8 return $impclass->_urn_init($self, $nid) if $impclass;
21              
22 2         4 $impclass = "URI::urn";
23 2 50       9 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         3 $id =~ s/-/_/g;
27 2 50       27 $id = "_$id" if $id =~ /^\d/;
28              
29 2         4 $impclass = "URI::urn::$id";
30 2     2   9 no strict 'refs';
  2         2  
  2         1026  
31 2 50       3 unless (@{"${impclass}::ISA"}) {
  2         14  
32             # Try to load it
33 2         2 my $_old_error = $@;
34 2         113 eval "require $impclass";
35 2 50 66     34 die $@ if $@ && $@ !~ /Can\'t locate.*in \@INC/;
36 2         2 $@ = $_old_error;
37 2 100       4 $impclass = "URI::urn" unless @{"${impclass}::ISA"};
  2         7  
38             }
39             }
40             else {
41 0 0       0 carp("Illegal namespace identifier '$nid' for URN '$self'") if $^W;
42             }
43 2         14 $implementor{$nid} = $impclass;
44 2         10 return $impclass->_urn_init($self, $nid);
45             }
46              
47             sub _urn_init {
48 3     3   5 my($class, $self, $nid) = @_;
49 3         10 bless $self, $class;
50             }
51              
52             sub _nid {
53 4     4   4 my $self = shift;
54 4         15 my $opaque = $self->opaque;
55 4 50       19 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         13 $opaque =~ s/:.*//s;
63 4         7 return $opaque;
64             }
65              
66             sub nid { # namespace identifier
67 4     4 0 16 my $self = shift;
68 4         11 my $nid = $self->_nid(@_);
69 4 50       10 $nid = lc($nid) if defined($nid);
70 4         9 return $nid;
71             }
72              
73             sub nss { # namespace specific string
74 5     5 0 6 my $self = shift;
75 5         12 my $opaque = $self->opaque;
76 5 100       11 if (@_) {
77 1         2 my $v = $opaque;
78 1         1 my $new = shift;
79 1 50       3 if (defined $new) {
80 1         6 $v =~ s/(:|\z).*/:$new/;
81             }
82             else {
83 0         0 $v =~ s/:.*//s;
84             }
85 1         2 $self->opaque($v);
86             }
87 5 100       25 return undef unless $opaque =~ s/^[^:]*://;
88 3         13 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;