File Coverage

blib/lib/SeeAlso/Identifier.pm
Criterion Covered Total %
statement 37 41 90.2
branch 8 10 80.0
condition n/a
subroutine 17 20 85.0
pod 13 13 100.0
total 75 84 89.2


line stmt bran cond sub pod time code
1 11     11   23526 use strict;
  11         49  
  11         406  
2 11     11   58 use warnings;
  11         22  
  11         1663  
3             package SeeAlso::Identifier;
4             {
5             $SeeAlso::Identifier::VERSION = '0.71';
6             }
7             #ABSTRACT: Controlled identifier that can be normalized and hashed
8              
9              
10             use overload (
11 22     22   3563 '""' => sub { $_[0]->as_string },
12 64     64   1248 'bool' => sub { $_[0]->valid },
13 2     2   6 '<=>' => sub { $_[0]->cmp( $_[1] ) },
14 38     38   3469 'cmp' => sub { $_[0]->cmp( $_[1] ) },
15 11         155 fallback => 1
16 11     11   5005 );
  11         3545  
17              
18              
19             sub parse {
20 101     101 1 207 my $value = shift;
21 101 100       917 return defined $value ? "$value" : "";
22             }
23              
24              
25             sub new {
26 127     127 1 3724 my $class = shift;
27              
28 127         185 my $value = '';
29 127         324 my $self = bless \$value, $class;
30 127         419 $self->value( $_[0] );
31              
32 127         400 return $self;
33             }
34              
35              
36             sub value {
37 175     175 1 2747 my $self = shift;
38 175 100       411 if ( scalar @_ ) {
39             ## no critic
40 141         8944 my $value = eval ref($self).'::parse($_[0])';
41             ## use critic
42 141 100       1875 $$self = defined $value ? "$value" : "";
43             }
44 175         452 return $$self;
45             }
46              
47              
48             sub canonical {
49 151     151 1 186 return ${$_[0]};
  151         923  
50             }
51              
52              
53             sub hash {
54 27     27 1 68 return $_[0]->canonical;
55             }
56              
57              
58             sub valid {
59 86     86 1 604 return ${$_[0]} ne '';
  86         463  
60             }
61              
62              
63              
64             sub cmp {
65 40     40 1 63 my $self = shift;
66 40         72 my $second = shift;
67             # TODO: use the same class as the first for comparing (and test this)!
68 40 100       217 $second = SeeAlso::Identifier->new( $second )
69             unless UNIVERSAL::isa( $second, 'SeeAlso::Identifier' );
70 40         154 return $self->canonical cmp $second->canonical;
71             }
72              
73              
74             sub as_string {
75 56     56 1 175 return $_[0]->canonical;
76             }
77              
78              
79 26     26 1 524 sub normalized { return $_[0]->canonical; }
80              
81 3     3 1 14 sub indexed { return $_[0]->hash; }
82              
83 0     0 1   sub condensed { return $_[0]->hash; }
84              
85 0     0 1   sub get { return $_[0]->value; }
86              
87             sub set {
88 0     0 1   my $self = shift;
89 0 0         return $self->value( scalar @_ ? @_ : undef );
90             }
91              
92             1;
93              
94              
95             __END__