<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package URI::WithBase;

use strict;
use warnings;

use URI ();
use Scalar::Util qw(blessed);

our $VERSION = '5.10';

use overload '""' =&gt; "as_string", fallback =&gt; 1;

sub as_string;  # help overload find it

sub new
{
    my($class, $uri, $base) = @_;
    my $ibase = $base;
    if ($base &amp;&amp; blessed($base) &amp;&amp; $base-&gt;isa(__PACKAGE__)) {
	$base = $base-&gt;abs;
	$ibase = $base-&gt;[0];
    }
    bless [URI-&gt;new($uri, $ibase), $base], $class;
}

sub new_abs
{
    my $class = shift;
    my $self = $class-&gt;new(@_);
    $self-&gt;abs;
}

sub _init
{
    my $class = shift;
    my($str, $scheme) = @_;
    bless [URI-&gt;new($str, $scheme), undef], $class;
}

sub eq
{
    my($self, $other) = @_;
    $other = $other-&gt;[0] if blessed($other) and $other-&gt;isa(__PACKAGE__);
    $self-&gt;[0]-&gt;eq($other);
}

our $AUTOLOAD;
sub AUTOLOAD
{
    my $self = shift;
    my $method = substr($AUTOLOAD, rindex($AUTOLOAD, '::')+2);
    return if $method eq "DESTROY";
    $self-&gt;[0]-&gt;$method(@_);
}

sub can {                                  # override UNIVERSAL::can
    my $self = shift;
    $self-&gt;SUPER::can(@_) || (
      ref($self)
      ? $self-&gt;[0]-&gt;can(@_)
      : undef
    )
}

sub base {
    my $self = shift;
    my $base  = $self-&gt;[1];

    if (@_) { # set
	my $new_base = shift;
	# ensure absoluteness
	$new_base = $new_base-&gt;abs if ref($new_base) &amp;&amp; $new_base-&gt;isa(__PACKAGE__);
	$self-&gt;[1] = $new_base;
    }
    return unless defined wantarray;

    # The base attribute supports 'lazy' conversion from URL strings
    # to URL objects. Strings may be stored but when a string is
    # fetched it will automatically be converted to a URL object.
    # The main benefit is to make it much cheaper to say:
    #   URI::WithBase-&gt;new($random_url_string, 'http:')
    if (defined($base) &amp;&amp; !ref($base)) {
	$base = ref($self)-&gt;new($base);
	$self-&gt;[1] = $base unless @_;
    }
    $base;
}

sub clone
{
    my $self = shift;
    my $base = $self-&gt;[1];
    $base = $base-&gt;clone if ref($base);
    bless [$self-&gt;[0]-&gt;clone, $base], ref($self);
}

sub abs
{
    my $self = shift;
    my $base = shift || $self-&gt;base || return $self-&gt;clone;
    $base = $base-&gt;as_string if ref($base);
    bless [$self-&gt;[0]-&gt;abs($base, @_), $base], ref($self);
}

sub rel
{
    my $self = shift;
    my $base = shift || $self-&gt;base || return $self-&gt;clone;
    $base = $base-&gt;as_string if ref($base);
    bless [$self-&gt;[0]-&gt;rel($base, @_), $base], ref($self);
}

1;

__END__

=head1 NAME

URI::WithBase - URIs which remember their base

=head1 SYNOPSIS

 $u1 = URI::WithBase-&gt;new($str, $base);
 $u2 = $u1-&gt;abs;

 $base = $u1-&gt;base;
 $u1-&gt;base( $new_base )

=head1 DESCRIPTION

This module provides the C&lt;URI::WithBase&gt; class.  Objects of this class
are like C&lt;URI&gt; objects, but can keep their base too.  The base
represents the context where this URI was found and can be used to
absolutize or relativize the URI.  All the methods described in L&lt;URI&gt;
are supported for C&lt;URI::WithBase&gt; objects.

The methods provided in addition to or modified from those of C&lt;URI&gt; are:

=over 4

=item $uri = URI::WithBase-&gt;new($str, [$base])

The constructor takes an optional base URI as the second argument.
If provided, this argument initializes the base attribute.

=item $uri-&gt;base( [$new_base] )

Can be used to get or set the value of the base attribute.
The return value, which is the old value, is a URI object or C&lt;undef&gt;.

=item $uri-&gt;abs( [$base_uri] )

The $base_uri argument is now made optional as the object carries its
base with it.  A new object is returned even if $uri is already
absolute (while plain URI objects simply return themselves in
that case).

=item $uri-&gt;rel( [$base_uri] )

The $base_uri argument is now made optional as the object carries its
base with it.  A new object is always returned.

=back


=head1 SEE ALSO

L&lt;URI&gt;

=head1 COPYRIGHT

Copyright 1998-2002 Gisle Aas.

=cut
</pre></body></html>