4 use vars qw($AUTOLOAD $VERSION);
9 use overload '""' => "as_string", fallback => 1;
11 sub as_string; # help overload find it
15 my($class, $uri, $base) = @_;
17 if ($base && ref($base) && UNIVERSAL::isa($base, __PACKAGE__)) {
21 bless [URI->new($uri, $ibase), $base], $class;
27 my $self = $class->new(@_);
34 my($str, $scheme) = @_;
35 bless [URI->new($str, $scheme), undef], $class;
40 my($self, $other) = @_;
41 $other = $other->[0] if UNIVERSAL::isa($other, __PACKAGE__);
42 $self->[0]->eq($other);
48 my $method = substr($AUTOLOAD, rindex($AUTOLOAD, '::')+2);
49 return if $method eq "DESTROY";
50 $self->[0]->$method(@_);
53 sub can { # override UNIVERSAL::can
55 $self->SUPER::can(@_) || (
64 my $base = $self->[1];
69 $new_base = $new_base->abs if ref($new_base) && $new_base->isa(__PACKAGE__);
70 $self->[1] = $new_base;
72 return unless defined wantarray;
74 # The base attribute supports 'lazy' conversion from URL strings
75 # to URL objects. Strings may be stored but when a string is
76 # fetched it will automatically be converted to a URL object.
77 # The main benefit is to make it much cheaper to say:
78 # URI::WithBase->new($random_url_string, 'http:')
79 if (defined($base) && !ref($base)) {
80 $base = ref($self)->new($base);
81 $self->[1] = $base unless @_;
89 my $base = $self->[1];
90 $base = $base->clone if ref($base);
91 bless [$self->[0]->clone, $base], ref($self);
97 my $base = shift || $self->base || return $self->clone;
98 $base = $base->as_string if ref($base);
99 bless [$self->[0]->abs($base, @_), $base], ref($self);
105 my $base = shift || $self->base || return $self->clone;
106 $base = $base->as_string if ref($base);
107 bless [$self->[0]->rel($base, @_), $base], ref($self);
116 URI::WithBase - URIs which remember their base
120 $u1 = URI::WithBase->new($str, $base);
124 $u1->base( $new_base )
128 This module provides the C<URI::WithBase> class. Objects of this class
129 are like C<URI> objects, but can keep their base too. The base
130 represents the context where this URI was found and can be used to
131 absolutize or relativize the URI. All the methods described in L<URI>
132 are supported for C<URI::WithBase> objects.
134 The methods provided in addition to or modified from those of C<URI> are:
138 =item $uri = URI::WithBase->new($str, [$base])
140 The constructor takes an optional base URI as the second argument.
141 If provided, this argument initializes the base attribute.
143 =item $uri->base( [$new_base] )
145 Can be used to get or set the value of the base attribute.
146 The return value, which is the old value, is a URI object or C<undef>.
148 =item $uri->abs( [$base_uri] )
150 The $base_uri argument is now made optional as the object carries its
151 base with it. A new object is returned even if $uri is already
152 absolute (while plain URI objects simply return themselves in
155 =item $uri->rel( [$base_uri] )
157 The $base_uri argument is now made optional as the object carries its
158 base with it. A new object is always returned.
169 Copyright 1998-2002 Gisle Aas.