SYNOPSIS

    use Rose::URI;

    $uri = Rose::URI->new('http://un:[email protected]/bar/baz?a=1&b=two+3');

    $scheme = $uri->scheme;
    $user   = $uri->username;
    $pass   = $uri->password;
    $host   = $uri->host;
    $path   = $uri->path;
    ...

    $b = $uri->query_param('b');  # $b = "two 3"
    $a = $uri->query_param('a');  # $a = 1

    $uri->query_param_delete('b');
    $uri->query_param('c' => 'blah blah');
    ...

    print $uri;

DESCRIPTION

Rose::URI is an alternative to \s-1URI\s0. The important differences are as follows.

Rose::URI provides a rich set of query string manipulation methods. Query parameters can be added, removed, and checked for their existence. \s-1URI\s0 allows the entire query to be set or returned as a whole via the query_form or query methods, and the URI::QueryParam module provides a few more methods for query string manipulation.

Rose::URI supports query parameters with multiple values (e.g. \*(L"a=1&a=2\*(R"). \s-1URI\s0 has limited support for this through query_form's list return value. Better methods are available in URI::QueryParam.

Rose::URI uses Apache's C-based \s-1URI\s0 parsing and \s-1HTML\s0 escaping functions when running in a mod_perl 1.x web server environment.

Rose::URI stores each \s-1URI\s0 \*(L"in pieces\*(R" (scheme, host, path, etc.) and then assembles those pieces when the entire \s-1URI\s0 is needed as a string. This technique is based on the assumption that the \s-1URI\s0 will be manipulated many more times than it is stringified. If this is not the case in your usage scenario, then \s-1URI\s0 may be a better alternative.

Now some similarities: both classes use the overload module to allow \*(L"magic\*(R" stringification. Both \s-1URI\s0 and Rose::URI objects can be printed and compared as if they were strings.

Rose::URI actually uses the \s-1URI\s0 class to do the heavy lifting of parsing URIs when not running in a mod_perl 1.x environment.

Finally, a caveat: Rose::URI supports only \*(L"http\*(R"-like URIs. This includes ftp, http, https, and other similar looking URIs. \s-1URI\s0 supports many more esoteric \s-1URI\s0 types (gopher, mailto, etc.) If you need to support these formats, use \s-1URI\s0 instead.

CONSTRUCTOR

new [ \s-1URI\s0 | \s-1PARAMS\s0 ]

Constructs a \s-1URI\s0 object based on \s-1URI\s0 or \s-1PARAMS\s0, where \s-1URI\s0 is a string and \s-1PARAMS\s0 are described below. Returns a new Rose::URI object. The query string portion of the \s-1URI\s0 argument may use either \*(L"&\*(R" or \*(L";\*(R" as the parameter separator. Examples: $uri = Rose::URI->new('/foo?a=1&b=2'); $uri = Rose::URI->new('/foo?a=1;b=2'); # same thing The query_param_separator parameter determines what is used when the query string (or the whole \s-1URI\s0) is output as a string later. Rose::URI uses \s-1URI\s0 or Apache::URI (when running under mod_perl 1.x) to do its \s-1URI\s0 string parsing. Valid \s-1PARAMS\s0 are: fragment host password path port query scheme username

query_param_separator Which correspond to the following \s-1URI\s0 pieces: <scheme>://<username:password>@<path>?<query>#<fragment> All the above parameters accept strings. See below for more information about the query parameter. The query_param_separator parameter determines the separator used when constructing the query string. It is \*(L"&\*(R" by default (e.g. \*(L"a=1&b=2\*(R")

CLASS METHODS

default_omit_empty_query_params [\s-1BOOL\s0]

Get or set a boolean value that determines whether or not query parameters with \*(L"empty\*(R" (that is, undef or zero-length) values will be omitted from the query string by default. The default value is false.

default_query_param_separator [\s-1CHARACTER\s0]

Get or set the character used to separate query parameters in the stringified version of Rose::URI objects. Defaults to \*(L"&\*(R".

OBJECT METHODS

abs [\s-1BASE\s0]

This method exists solely for compatibility with \s-1URI\s0. Returns an absolute Rose::URI object. If the current \s-1URI\s0 is already absolute, then a reference to it is simply returned. If the current \s-1URI\s0 is relative, then a new absolute \s-1URI\s0 is constructed by combining the \s-1URI\s0 and the \s-1BASE\s0, and returned.

as_string

Returns the \s-1URI\s0 as a string. The string is \*(L"\s-1URI\s0 escaped\*(R" (reserved \s-1URI\s0 characters are replaced with %xx sequences), but not \*(L"\s-1HTML\s0 escaped\*(R" (ampersands are not escaped, for example).

clone

Returns a copy of the Rose::URI object.

fragment [\s-1FRAGMENT\s0]

Get or set the fragment portion of the \s-1URI\s0.

omit_empty_query_params [\s-1BOOL\s0]

Get or set a boolean value that determines whether or not query parameters with \*(L"empty\*(R" (that is, undef or zero-length) values will be omitted from the query string. The default value is determined by the default_query_param_separator class method.

password [\s-1PASSWORD\s0]

Get or set the password portion of the \s-1URI\s0.

path [\s-1PATH\s0]

Get or set the path portion of the \s-1URI\s0.

port [\s-1PORT\s0]

Get or set the port number portion of the \s-1URI\s0.

query [\s-1QUERY\s0]

Get or sets the \s-1URI\s0's query. \s-1QUERY\s0 may be an appropriately escaped query string (e.g. \*(L"a=1&b=2&c=a+long+string\*(R"), a reference to a hash, or a list of name/value pairs. Query strings may use either \*(L"&\*(R" or \*(L";\*(R" as their query separator. If a \*(L"&\*(R" character exists anywhere in the query string, it is assumed to be the separator. If none of the characters \*(L"&\*(R", \*(L";\*(R", or \*(L"=\*(R" appears in the query string, then the entire query string is taken as a single parameter name with an undefined value. Hashes and lists should specify multiple parameter values using array references. Here are some examples representing the query string \*(L"a=1&a=2&b=3\*(R" $uri->query("a=1&a=2&b=3"); # string $uri->query("a=1;a=2;b=3"); # same thing $uri->query({ a => [ 1, 2 ], b => 3 }); # hash ref $uri->query(a => [ 1, 2 ], b => 3); # list Returns the current (or new) query as a URI-escaped (but not HTML-escaped) query string.

query_form \s-1QUERY\s0

Implementation of \s-1URI\s0's method of the same name. This exists for backwards compatibility purposes only and should not be used (or necessary). See the \s-1URI\s0 documentation for more details.

query_hash

Returns the current query as a hash (in list context) or reference to a hash (in scalar context), with multiple parameter values represented by array references (see the query method for details). The return value is a shallow copy of the actual query hash. It should be treated as read-only unless you really know what you are doing. Example: $uri = Rose::URI->new('/foo?a=1&b=2&a=2');

$h = $uri->query_hash; # $h = { a => [ 1, 2 ], b => 2 }

query_param \s-1NAME\s0 [, \s-1VALUE\s0]

Get or set a query parameter. If only \s-1NAME\s0 is passed, it returns the value of the query parameter named \s-1NAME\s0. Parameters with multiple values are returned as array references. If both \s-1NAME\s0 and \s-1VALUE\s0 are passed, it sets the parameter named \s-1NAME\s0 to \s-1VALUE\s0, where \s-1VALUE\s0 can be a simple scalar value or a reference to an array of simple scalar values. Examples: $uri = Rose::URI->new('/foo?a=1');

$a = $uri->query_param('a'); # $a = 1

$uri->query_param('a' => 3); # query string is now "a=3"

$uri->query_param('b' => [ 4, 5 ]); # now "a=3&b=4&b=5"

$b = $uri->query_param('b'); # $b = [ 4, 5 ];

query_params \s-1NAME\s0 [, \s-1VALUE\s0]

Same as the query_param method, except the return value is always either an array (in list context) or reference to an array (in scalar context), even if there is only one value. Examples: $uri = Rose::URI->new('/foo?a=1&b=1&b=2');

$a = $uri->query_params('a'); # $a = [ 1 ] @a = $uri->query_params('a'); # @a = ( 1 )

$b = $uri->query_params('a'); # $b = [ 1, 2 ] @b = $uri->query_params('a'); # @b = ( 1, 2 )

query_param_add \s-1NAME\s0, \s-1VALUE\s0

Adds a new value to a query parameter. Example: $uri = Rose::URI->new('/foo?a=1&b=1');

$a = $uri->query_param_add('b' => 2); # now "a=2&b=1&b=2" Returns an array (in list context) or reference to an array (in scalar context) of the new parameter value(s).

query_param_delete \s-1NAME\s0

Deletes all instances of the parameter named \s-1NAME\s0 from the query.

query_param_exists \s-1NAME\s0

Returns a boolean value indicating whether or not a parameter named \s-1NAME\s0 exists in the query string.

query_param_separator [\s-1CHARACTER\s0]

Get or set the character used to separate query parameters in the stringified version of the \s-1URI\s0. Defaults to the return value of the default_query_param_separator class method (\*(L"&\*(R" by default).

rel \s-1BASE\s0

This method exists solely for compatibility with \s-1URI\s0. Returns a relative \s-1URI\s0 reference if it is possible to make one that denotes the same resource relative to \s-1BASE\s0. If not, then the current \s-1URI\s0 is simply returned.

scheme [\s-1SCHEME\s0]

Get or set the scheme portion of the \s-1URI\s0.

userinfo

Returns the username and password attributes joined by a \*(L":\*(R" (colon). The username and password are not escaped in any way. If there is no password, only the username is returned (without the colon). If neither exist, an empty string is returned.

userinfo_escaped

Returns the username and password attributes joined by a \*(L":\*(R" (colon). The username and password are URI-escaped, but not HTML-escaped. If there is no password, only the username is returned (without the colon). If neither exist, an empty string is returned.

username [\s-1USERNAME\s0]

Get or set the username portion of the \s-1URI\s0.

AUTHOR

John C. Siracusa ([email protected])

LICENSE

Copyright (c) 2010 by John C. Siracusa. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.