~ubuntu-branches/ubuntu/hardy/liburi-perl/hardy

« back to all changes in this revision

Viewing changes to URI/WithBase.pm

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Hornburg (Racke)
  • Date: 2005-04-07 21:30:51 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20050407213051-p5tx197ayyr0u93p
Tags: 1.35-1
* new upstream release (Closes: #303588, thanks to Chip Salzenberg
  <chip@debian.org> for the report)
* changed Build-Depends into Build-Depends-Indep (Closes: #274195, thanks
  to Stephen Quinney <stephen@jadevine.org.uk> for the report)
* point to license files under /usr/share/common-licenses
* removed some cruft from debian/rules

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
package URI::WithBase;
2
2
 
3
3
use strict;
4
 
use vars qw($AUTOLOAD);
 
4
use vars qw($AUTOLOAD $VERSION);
5
5
use URI;
6
6
 
 
7
$VERSION = "2.19";
 
8
 
7
9
use overload '""' => "as_string", fallback => 1;
8
10
 
9
11
sub as_string;  # help overload find it
12
14
{
13
15
    my($class, $uri, $base) = @_;
14
16
    my $ibase = $base;
15
 
    if ($base && ref($base) && UNIVERSAL::isa($base, "URI::WithBase")) {
 
17
    if ($base && ref($base) && UNIVERSAL::isa($base, __PACKAGE__)) {
16
18
        $base = $base->abs;
17
19
        $ibase = $base->[0];
18
20
    }
36
38
sub eq
37
39
{
38
40
    my($self, $other) = @_;
39
 
    $other = $other->[0] if UNIVERSAL::isa($other, "URI::WithBase");
 
41
    $other = $other->[0] if UNIVERSAL::isa($other, __PACKAGE__);
40
42
    $self->[0]->eq($other);
41
43
}
42
44
 
63
65
 
64
66
    if (@_) { # set
65
67
        my $new_base = shift;
66
 
        $new_base = $new_base->abs if ref($new_base);  # ensure absoluteness
 
68
        # ensure absoluteness
 
69
        $new_base = $new_base->abs if ref($new_base) && $new_base->isa(__PACKAGE__);
67
70
        $self->[1] = $new_base;
68
71
    }
69
72
    return unless defined wantarray;
74
77
    # The main benefit is to make it much cheaper to say:
75
78
    #   URI::WithBase->new($random_url_string, 'http:')
76
79
    if (defined($base) && !ref($base)) {
77
 
        $base = URI->new($base);
 
80
        $base = ref($self)->new($base);
78
81
        $self->[1] = $base unless @_;
79
82
    }
80
83
    $base;
92
95
{
93
96
    my $self = shift;
94
97
    my $base = shift || $self->base || return $self->clone;
 
98
    $base = $base->as_string if ref($base);
95
99
    bless [$self->[0]->abs($base, @_), $base], ref($self);
96
100
}
97
101
 
99
103
{
100
104
    my $self = shift;
101
105
    my $base = shift || $self->base || return $self->clone;
 
106
    $base = $base->as_string if ref($base);
102
107
    bless [$self->[0]->rel($base, @_), $base], ref($self);
103
108
}
104
109
 
108
113
 
109
114
=head1 NAME
110
115
 
111
 
URI::WithBase - URI which remember their base
 
116
URI::WithBase - URIs which remember their base
112
117
 
113
118
=head1 SYNOPSIS
114
119
 
120
125
 
121
126
=head1 DESCRIPTION
122
127
 
123
 
This module provide the C<URI::WithBase> class.  Objects of this class
124
 
are like C<URI> objects, but can keep their base too.
 
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.
125
133
 
126
134
The methods provided in addition to or modified from those of C<URI> are:
127
135
 
129
137
 
130
138
=item $uri = URI::WithBase->new($str, [$base])
131
139
 
132
 
The constructor takes a an optional base URI as the second argument.
 
140
The constructor takes an optional base URI as the second argument.
 
141
If provided, this argument initializes the base attribute.
133
142
 
134
143
=item $uri->base( [$new_base] )
135
144
 
136
 
This method can be used to get or set the value of the base attribute.
 
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>.
137
147
 
138
148
=item $uri->abs( [$base_uri] )
139
149
 
140
 
The $base_uri argument is now made optional as the object carries it's
141
 
base with it.
 
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
 
153
that case).
142
154
 
143
155
=item $uri->rel( [$base_uri] )
144
156
 
145
 
The $base_uri argument is now made optional as the object carries it's
146
 
base with it.
 
157
The $base_uri argument is now made optional as the object carries its
 
158
base with it.  A new object is always returned.
147
159
 
148
160
=back
149
161
 
154
166
 
155
167
=head1 COPYRIGHT
156
168
 
157
 
Copyright 1998-2000 Gisle Aas.
 
169
Copyright 1998-2002 Gisle Aas.
158
170
 
159
171
=cut