URI-Builder
view release on metacpan or search on metacpan
* userinfo
* host
* port
* path_segments
* query_form
* query_keywords
* fragment
In addition the "query_separator" attribute defines how "query_form"
fields are joined. It defaults to ';' but can be usefully set to '&'.
The accessors for these attributes have a similar interface to the URI
methods, that is to say that they return old values when new ones are
set. Those attributes that take a list of values: "path_segments",
"query_form" and "query_keywords" all return plain lists but can be
passed nested array references.
METHODS
new
The constructor.
In addition to the attributes listed above, a "uri" argument can be
passed as a string or a URI object, which will be parsed to popoulate
any missing fields.
path
Returns the path portion of the URI as a string.
Can be assigned to to populate "path_segments".
Leading, trailing and doubled slashes are represented faithfully using
empty path segments.
query
Returns a string representation of the query. This is obtained from
either "query_form" or "query_keywords", in that order.
If an argument is passed, it is parsed to populate "query_form".
path_query
Returns a string representation of the path plus the query string. See
"path_query" in URI.
query_param
@keys = $uri->query_param
@values = $uri->query_param($key)
lib/URI/Builder.pm view on Meta::CPAN
=item * userinfo
=item * host
=item * port
=item * path_segments
=item * query_form
=item * query_keywords
=item * fragment
=back
In addition the C<query_separator> attribute defines how C<query_form> fields
are joined. It defaults to C<';'> but can be usefully set to '&'.
The accessors for these attributes have a similar interface to the L<URI>
methods, that is to say that they return old values when new ones are set.
Those attributes that take a list of values: C<path_segments>, C<query_form>
and C<query_keywords> all return plain lists but can be passed nested array
references.
=cut
my (@uri_fields, %listish, @fields);
BEGIN {
# Fields that correspond to methods in URI
@uri_fields = qw(
scheme
userinfo
host
port
path_segments
query_form
query_keywords
fragment
);
# Fields that contain lists of values
%listish = map { $_ => 1 } qw(
path_segments
query_form
query_keywords
);
# All fields
@fields = ( @uri_fields, qw( query_separator ));
# Generate accessors for all fields:
for my $field (@fields) {
my $glob = do { no strict 'refs'; \*$field };
*$glob = $listish{$field} ? sub {
lib/URI/Builder.pm view on Meta::CPAN
my @segments = split '/', shift, -1;
$self->path_segments(@segments);
}
return $old;
}
=head2 query
Returns a string representation of the query. This is obtained from either
C<query_form> or C<query_keywords>, in that order.
If an argument is passed, it is parsed to populate C<query_form>.
=cut
sub query {
my ($self, $query) = @_;
my @new;
if ($query) {
lib/URI/Builder.pm view on Meta::CPAN
}
my $old;
if (my @form = $self->query_form) {
push @form, '' if @form % 2;
my $uri = URI->new;
$uri->query_form(\@form, $self->query_separator);
$old = $uri->query();
}
else {
$old = join '+', $self->query_keywords;
}
$self->query_form(@new);
return $old;
}
=head2 path_query
Returns a string representation of the path plus the query string. See
lib/URI/Builder.pm view on Meta::CPAN
via URI::_server -> URI::_generic -> URI: fragment
via URI::_server -> URI::_generic -> URI: implementor
via URI::_server -> URI::_generic -> URI: new
via URI::_server -> URI::_generic -> URI: new_abs
via URI::_server -> URI::_generic -> URI: opaque
via URI::_server -> URI::_generic -> URI: scheme
via URI::_server -> URI::_generic -> URI: secure
via URI::_server -> URI::_generic -> URI::_query: equery
via URI::_server -> URI::_generic -> URI::_query: query
via URI::_server -> URI::_generic -> URI::_query: query_form
via URI::_server -> URI::_generic -> URI::_query: query_keywords
t/construct.t view on Meta::CPAN
scheme => 'http',
},
expect => 'http://localhost/one/two',
}, {
args => {
query_form => [ foo => 1, bar => 2, baz => 3 ],
},
expect => '?foo=1;bar=2;baz=3',
}, {
args => {
query_keywords => [qw( a b c )],
},
expect => '?a+b+c',
}, {
args => {
query_keywords => [qw( a b c )],
query_form => [ foo => 1, bar => 2, baz => 3 ],
},
expect => '?foo=1;bar=2;baz=3',
}, {
args => {
query_keywords => 'foo',
},
expect => '?foo',
}, {
args => {
query_keywords => 'foo',
host => 'localhost',
},
expect => '//localhost?foo',
}, {
args => {
query_keywords => 'foo',
host => 'localhost',
path => '/',
},
expect => '//localhost/?foo',
}, {
args => {
query_keywords => 'foo',
host => 'localhost',
path_segments => [qw( one two three )],
},
expect => '//localhost/one/two/three?foo',
}, {
args => {
# one pair only to avoid relying on hash order
query_form => { a => "b" },
},
expect => '?a=b',
( run in 2.631 seconds using v1.01-cache-2.11-cpan-299005ec8e3 )