Rose::URI(3pm)
π NAME π
okay so bestie, meet Rose::URI β it's a URI class that lets you do easy and efficient manipulation of URI components. yes, the whole vibe is "I want to poke at the pieces of a URL without losing my mind." π
β¨ SYNOPSIS β¨
use Rose::URI;
$uri = Rose::URI->new('http://un:pw@foo.com/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 πΈ
so Rose::URI is an alternative to URI. the important differences are, like, as follows. π«Ά
Rose::URIgives you a rich set of query string manipulation methods. Query parameters can be added, removed, and checked for their existence.URIlets you set or return the entire query as a whole via thequery_formorquerymethods, and theURI::QueryParammodule provides a few more methods for query string manipulation.Rose::URIsupports query parameters with multiple values (e.g."a=1&a=2").URIhas limited support for this throughquery_form's list return value. Better methods are available inURI::QueryParam.Rose::URIuses Apache's C-based URI parsing and HTML escaping functions when running in amod_perl1.x web server environment.Rose::URIstores each URI "in pieces" (scheme, host, path, etc.) and then assembles those pieces when the entire URI is needed as a string. this technique is based on the assumption that the URI will be manipulated many more times than it is stringified. if that's not your usage scenario, thenURImay be a better alternative, ngl.- now some similarities, because we're fair like that: both classes use the
overloadmodule to allow "magic" stringification. bothURIandRose::URIobjects can be printed and compared as if they were strings. β¨ Rose::URIactually uses theURIclass to do the heavy lifting of parsing URIs when not running in amod_perl1.x environment.
and finally, a caveat, because we keep it real: Rose::URI supports only "http"-like URIs. this includes ftp, http, https, and other similar looking URIs. URI supports many more esoteric URI types (gopher, mailto, etc.) if you need to support these formats, use URI instead. π
not me warning you twice: if you're doinggopherormailtononsense, this is not the class for you. useURI. periodt.
π CONSTRUCTOR π
new [ URI | PARAMS ]
Constructs a URI object based on URI or PARAMS, where URI is a string and PARAMS are described below. Returns a new Rose::URI object. π
the query string portion of the URI argument may use either "&" or ";" as the parameter separator. examples, bestie:
$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 URI) is output as a string later.
Rose::URI uses URI or Apache::URI (when running under mod_perl 1.x) to do its URI string parsing.
Valid PARAMS are:
fragmenthostpasswordpathportqueryschemeusernamequery_param_separator
which correspond to the following URI 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 "&" by default (e.g. "a=1&b=2"). π
π CLASS METHODS π
default_omit_empty_query_params [BOOL]β get or set a boolean value that determines whether or not query parameters with"empty"(that is,undefor zero-length) values will be omitted from the query string by default. the default value is false.default_query_param_separator [CHARACTER]β get or set the character used to separate query parameters in the stringified version ofRose::URIobjects. defaults to"&".
π¦ OBJECT METHODS π¦
abs [BASE]β this method exists solely for compatibility withURI. Returns an absoluteRose::URIobject. if the current URI is already absolute, then a reference to it is simply returned. if the current URI is relative, then a new absolute URI is constructed by combining the URI and the BASE, and returned.as_stringβ returns the URI as a string. the string is "URI escaped" (reserved URI characters are replaced with%xxsequences), but not "HTML escaped" (ampersands are not escaped, for example).cloneβ returns a copy of theRose::URIobject.fragment [FRAGMENT]β get or set the fragment portion of the URI.omit_empty_query_params [BOOL]β get or set a boolean value that determines whether or not query parameters with"empty"(that is,undefor zero-length) values will be omitted from the query string. the default value is determined by thedefault_query_param_separatorclass method.password [PASSWORD]β get or set the password portion of the URI.path [PATH]β get or set the path portion of the URI.port [PORT]β get or set the port number portion of the URI.query [QUERY]β get or sets the URI's query. QUERY may be an appropriately escaped query string (e.g."a=1&b=2&c=a+long+string"), a reference to a hash, or a list of name/value pairs.
query strings may use either "&" or ";" as their query separator. if a "&" character exists anywhere in the query string, it is assumed to be the separator.
if none of the characters "&", ";", or "=" 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 "a=1&a=2&b=3":
$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 QUERYβ implementation ofURI's method of the same name. this exists for backwards compatibility purposes only and should not be used (or necessary). see theURIdocumentation 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 thequerymethod 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 NAME [, VALUE]β get or set a query parameter. if only NAME is passed, it returns the value of the query parameter named NAME. parameters with multiple values are returned as array references. if both NAME and VALUE are passed, it sets the parameter named NAME to VALUE, where VALUE 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 NAME [, VALUE]β same as thequery_parammethod, 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 NAME, VALUEβ 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 NAMEβ deletes all instances of the parameter named NAME from the query.query_param_exists NAMEβ returns a boolean value indicating whether or not a parameter named NAME exists in the query string.query_param_separator [CHARACTER]β get or set the character used to separate query parameters in the stringified version of the URI. defaults to the return value of thedefault_query_param_separatorclass method ("&"by default).rel BASEβ this method exists solely for compatibility withURI. Returns a relative URI reference if it is possible to make one that denotes the same resource relative to BASE. if not, then the current URI is simply returned.scheme [SCHEME]β get or set the scheme portion of the URI.userinfoβ returns theusernameandpasswordattributes joined by a":"(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 theusernameandpasswordattributes joined by a":"(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 [USERNAME]β get or set the username portion of the URI.
π AUTHOR π
John C. Siracusa (siracusa@gmail.com) β absolute icon, we stan. π
π 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.
okay and that's the whole page bestie π this doc lives in perl v5.34.0, dated 2022-06-17 β that's the Rose::URI(3pm) manual page, and now you know it, so go manipulate some URIs. slay, hydrate, and remember: use URI for the gopher nonsense. πβ¨π¦
π¦ where this page comes from
- debian bookworm: librose-uri-perl 1.02-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: librose-uri-perl 1.02-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: librose-uri-perl 1.02-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: librose-uri-perl 1.02-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: librose-uri-perl 1.02-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms