File Coverage

blib/lib/URI/WithBase.pm
Criterion Covered Total %
statement 59 59 100.0
branch 21 26 80.7
condition 15 19 78.9
subroutine 15 15 100.0
pod 4 8 50.0
total 114 127 89.7


line stmt bran cond sub pod time code
1             package URI::WithBase;
2              
3 6     6   2607 use strict;
  6         12  
  6         171  
4 6     6   30 use warnings;
  6         14  
  6         132  
5              
6 6     6   2871 use URI ();
  6         18  
  6         200  
7 6     6   57 use Scalar::Util qw(blessed);
  6         14  
  6         508  
8              
9             our $VERSION = '5.21';
10              
11 6     6   45 use overload '""' => "as_string", fallback => 1;
  6         12  
  6         31  
12              
13             sub as_string; # help overload find it
14              
15             sub new
16             {
17 257     257 1 610 my($class, $uri, $base) = @_;
18 257         433 my $ibase = $base;
19 257 100 100     897 if ($base && blessed($base) && $base->isa(__PACKAGE__)) {
      100        
20 9         39 $base = $base->abs;
21 9         20 $ibase = $base->[0];
22             }
23 257         683 bless [URI->new($uri, $ibase), $base], $class;
24             }
25              
26             sub new_abs
27             {
28 1     1 0 3 my $class = shift;
29 1         5 my $self = $class->new(@_);
30 1         4 $self->abs;
31             }
32              
33             sub _init
34             {
35 2     2   6 my $class = shift;
36 2         5 my($str, $scheme) = @_;
37 2         19 bless [URI->new($str, $scheme), undef], $class;
38             }
39              
40             sub eq
41             {
42 10     10 0 62 my($self, $other) = @_;
43 10 100 66     68 $other = $other->[0] if blessed($other) and $other->isa(__PACKAGE__);
44 10         46 $self->[0]->eq($other);
45             }
46              
47             our $AUTOLOAD;
48             sub AUTOLOAD
49             {
50 1167     1167   102149 my $self = shift;
51 1167         2381 my $method = substr($AUTOLOAD, rindex($AUTOLOAD, '::')+2);
52 1167 100       4130 return if $method eq "DESTROY";
53 810         2506 $self->[0]->$method(@_);
54             }
55              
56             sub can { # override UNIVERSAL::can
57 2     2 0 9 my $self = shift;
58 2 50       97 $self->SUPER::can(@_) || (
    50          
59             ref($self)
60             ? $self->[0]->can(@_)
61             : undef
62             )
63             }
64              
65             sub base {
66 86     86 1 956 my $self = shift;
67 86         128 my $base = $self->[1];
68              
69 86 100       176 if (@_) { # set
70 1         1 my $new_base = shift;
71             # ensure absoluteness
72 1 50 33     7 $new_base = $new_base->abs if ref($new_base) && $new_base->isa(__PACKAGE__);
73 1         3 $self->[1] = $new_base;
74             }
75 86 100       185 return unless defined wantarray;
76              
77             # The base attribute supports 'lazy' conversion from URL strings
78             # to URL objects. Strings may be stored but when a string is
79             # fetched it will automatically be converted to a URL object.
80             # The main benefit is to make it much cheaper to say:
81             # URI::WithBase->new($random_url_string, 'http:')
82 85 100 100     350 if (defined($base) && !ref($base)) {
83 63         190 $base = ref($self)->new($base);
84 63 50       170 $self->[1] = $base unless @_;
85             }
86 85         462 $base;
87             }
88              
89             sub clone
90             {
91 7     7 0 23 my $self = shift;
92 7         16 my $base = $self->[1];
93 7 100       20 $base = $base->clone if ref($base);
94 7         33 bless [$self->[0]->clone, $base], ref($self);
95             }
96              
97             sub abs
98             {
99 82     82 1 116 my $self = shift;
100 82   100     244 my $base = shift || $self->base || return $self->clone;
101 77 100       371 $base = $base->as_string if ref($base);
102 77         294 bless [$self->[0]->abs($base, @_), $base], ref($self);
103             }
104              
105             sub rel
106             {
107 15     15 1 24 my $self = shift;
108 15   50     52 my $base = shift || $self->base || return $self->clone;
109 15 50       80 $base = $base->as_string if ref($base);
110 15         62 bless [$self->[0]->rel($base, @_), $base], ref($self);
111             }
112              
113             1;
114              
115             __END__