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   2401 use strict;
  6         13  
  6         167  
4 6     6   29 use warnings;
  6         11  
  6         130  
5              
6 6     6   2613 use URI ();
  6         14  
  6         158  
7 6     6   41 use Scalar::Util qw(blessed);
  6         13  
  6         404  
8              
9             our $VERSION = '5.19';
10              
11 6     6   38 use overload '""' => "as_string", fallback => 1;
  6         12  
  6         24  
12              
13             sub as_string; # help overload find it
14              
15             sub new
16             {
17 257     257 1 626 my($class, $uri, $base) = @_;
18 257         366 my $ibase = $base;
19 257 100 100     946 if ($base && blessed($base) && $base->isa(__PACKAGE__)) {
      100        
20 9         31 $base = $base->abs;
21 9         21 $ibase = $base->[0];
22             }
23 257         735 bless [URI->new($uri, $ibase), $base], $class;
24             }
25              
26             sub new_abs
27             {
28 1     1 0 3 my $class = shift;
29 1         4 my $self = $class->new(@_);
30 1         4 $self->abs;
31             }
32              
33             sub _init
34             {
35 2     2   4 my $class = shift;
36 2         4 my($str, $scheme) = @_;
37 2         8 bless [URI->new($str, $scheme), undef], $class;
38             }
39              
40             sub eq
41             {
42 10     10 0 50 my($self, $other) = @_;
43 10 100 66     69 $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   112553 my $self = shift;
51 1167         2363 my $method = substr($AUTOLOAD, rindex($AUTOLOAD, '::')+2);
52 1167 100       4061 return if $method eq "DESTROY";
53 810         2747 $self->[0]->$method(@_);
54             }
55              
56             sub can { # override UNIVERSAL::can
57 2     2 0 9 my $self = shift;
58 2 50       100 $self->SUPER::can(@_) || (
    50          
59             ref($self)
60             ? $self->[0]->can(@_)
61             : undef
62             )
63             }
64              
65             sub base {
66 86     86 1 1390 my $self = shift;
67 86         142 my $base = $self->[1];
68              
69 86 100       162 if (@_) { # set
70 1         2 my $new_base = shift;
71             # ensure absoluteness
72 1 50 33     8 $new_base = $new_base->abs if ref($new_base) && $new_base->isa(__PACKAGE__);
73 1         2 $self->[1] = $new_base;
74             }
75 86 100       191 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     302 if (defined($base) && !ref($base)) {
83 63         201 $base = ref($self)->new($base);
84 63 50       175 $self->[1] = $base unless @_;
85             }
86 85         429 $base;
87             }
88              
89             sub clone
90             {
91 7     7 0 17 my $self = shift;
92 7         14 my $base = $self->[1];
93 7 100       21 $base = $base->clone if ref($base);
94 7         26 bless [$self->[0]->clone, $base], ref($self);
95             }
96              
97             sub abs
98             {
99 82     82 1 130 my $self = shift;
100 82   100     265 my $base = shift || $self->base || return $self->clone;
101 77 100       371 $base = $base->as_string if ref($base);
102 77         328 bless [$self->[0]->abs($base, @_), $base], ref($self);
103             }
104              
105             sub rel
106             {
107 15     15 1 28 my $self = shift;
108 15   50     47 my $base = shift || $self->base || return $self->clone;
109 15 50       80 $base = $base->as_string if ref($base);
110 15         71 bless [$self->[0]->rel($base, @_), $base], ref($self);
111             }
112              
113             1;
114              
115             __END__