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 7     7   3798 use strict;
  7         11  
  7         252  
4 7     7   49 use warnings;
  7         11  
  7         281  
5              
6 7     7   2505 use URI ();
  7         17  
  7         185  
7 7     7   42 use Scalar::Util qw(blessed);
  7         9  
  7         558  
8              
9             our $VERSION = '5.34';
10              
11 7     7   36 use overload '""' => "as_string", fallback => 1;
  7         11  
  7         32  
12              
13             sub as_string; # help overload find it
14              
15             sub new
16             {
17 257     257 1 623 my($class, $uri, $base) = @_;
18 257         345 my $ibase = $base;
19 257 100 100     829 if ($base && blessed($base) && $base->isa(__PACKAGE__)) {
      100        
20 9         32 $base = $base->abs;
21 9         17 $ibase = $base->[0];
22             }
23 257         773 bless [URI->new($uri, $ibase), $base], $class;
24             }
25              
26             sub new_abs
27             {
28 1     1 0 4 my $class = shift;
29 1         5 my $self = $class->new(@_);
30 1         5 $self->abs;
31             }
32              
33             sub _init
34             {
35 2     2   3 my $class = shift;
36 2         4 my($str, $scheme) = @_;
37 2         6 bless [URI->new($str, $scheme), undef], $class;
38             }
39              
40             sub eq
41             {
42 10     10 0 42 my($self, $other) = @_;
43 10 100 66     64 $other = $other->[0] if blessed($other) and $other->isa(__PACKAGE__);
44 10         38 $self->[0]->eq($other);
45             }
46              
47             our $AUTOLOAD;
48             sub AUTOLOAD
49             {
50 1169     1169   138764 my $self = shift;
51 1169         2359 my $method = substr($AUTOLOAD, rindex($AUTOLOAD, '::')+2);
52 1169 100       3760 return if $method eq "DESTROY";
53 812         2613 $self->[0]->$method(@_);
54             }
55              
56             sub can { # override UNIVERSAL::can
57 2     2 0 7 my $self = shift;
58 2 50       294 $self->SUPER::can(@_) || (
    50          
59             ref($self)
60             ? $self->[0]->can(@_)
61             : undef
62             )
63             }
64              
65             sub base {
66 86     86 1 1244 my $self = shift;
67 86         128 my $base = $self->[1];
68              
69 86 100       174 if (@_) { # set
70 1         1 my $new_base = shift;
71             # ensure absoluteness
72 1 50 33     3 $new_base = $new_base->abs if ref($new_base) && $new_base->isa(__PACKAGE__);
73 1         2 $self->[1] = $new_base;
74             }
75 86 100       167 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     284 if (defined($base) && !ref($base)) {
83 63         165 $base = ref($self)->new($base);
84 63 50       203 $self->[1] = $base unless @_;
85             }
86 85         460 $base;
87             }
88              
89             sub clone
90             {
91 7     7 0 15 my $self = shift;
92 7         10 my $base = $self->[1];
93 7 100       22 $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 108 my $self = shift;
100 82   100     235 my $base = shift || $self->base || return $self->clone;
101 77 100       360 $base = $base->as_string if ref($base);
102 77         261 bless [$self->[0]->abs($base, @_), $base], ref($self);
103             }
104              
105             sub rel
106             {
107 15     15 1 18 my $self = shift;
108 15   50     47 my $base = shift || $self->base || return $self->clone;
109 15 50       70 $base = $base->as_string if ref($base);
110 15         65 bless [$self->[0]->rel($base, @_), $base], ref($self);
111             }
112              
113             1;
114              
115             __END__