| Previous | Next
The URI ModuleThe URI module contains functions and modules to specify and convert URIs. (URLs are a type of URI.) In addition to the URL module itself, there are also: URI::URL, URI::Escape, and URI::Heuristic. Of primary importance to many LWP applications is the URI::URL class, which creates the objects used by LWP::UserAgent to determine protocols, server locations, and resource names. The URI::Escape module replaces unsafe characters in URL strings with their appropriate escape sequences. URI::Heuristic provides convenience methods for creating proper URLs out of short strings and incomplete addresses. URIThe URI module is a successor to URI::URL and was written by Gisle Aas. While not clearly stated in the LWP documentation, you should use the URI module whenever possible, since URI.pm has essentially deprecated URI::URL. The URI module implements the URI class. Objects created from the URI class represent Uniform Resource Identifiers (URIs). With the URI module, you can identify the key parts of a URI: scheme, scheme-specific parts, and fragment identifiers, which may be referred to respectfully as authority, path, and query components. For example, as shown in the URI module documentation: <scheme>:<scheme-specific-part>#<fragment> <scheme>://<authority><path>?<query>#<fragment> <path>?<query>#<fragment> You can break down http://www.anonymous.com/somefile.html as: scheme: http authority: www.anonymous.com path: /somefile.html In the case of relative URIs, you can use the URI module to deal with only the query component of a URI. With the URI module, you can parse the above URI as follows: #!/usr/local/bin/perl -w use URI; my $url = 'http://www.anonymous.com/somefile.html'; my $u1 = URI->new($url); print "scheme: ", $u1->scheme, "\n"; print "authority: ", $u1->authority, "\n"; print "path: ", $u1->path, "\n"; URI methodsThe following methods give you access to components of a URI. These methods will return a string, unless the URI component is invalid, in which case
new($uri, [$scheme]) Constructor.
URI::file->new($file, [$os]) Constructs a new file URI from a filename.
URI::file->new_abs($file, [$os]) Constructs a new absolute file URI from a filename.
abs($base_uri) Returns an absolute URI reference. If
as_string Returns a URI object as a plain string.
authority([$auth]) Sets and gets the authority component of the
canonical Returns a normalized version of the URI. This includes lowercasing the scheme and hostname components, as well as removing an explicit port specification (if it mtaches the default port).
clone Returns a copy of the URI.
URI::file->cwd Returns the current working directory as a file URI.
default_port() Returns the default port of the URI scheme that
eq() Compares two URIs.
fragment([$new_frag]) Returns the fragment identifier of a URI reference as an escaped string.
host([$new_host]) Sets and gets the unescaped hostname. To specify a different port: $new_host = "hostname:port_number"
host_port([$new_host_port]) Sets and gets the host and port as a single unit. Hostname and port are colon-separated.
new_abs($str, $base_uri) Creates a new absolute URI object.
opaque([$new_opaque_value]) Sets and gets the scheme-specific part of
path([$path]) Sets and gets the escaped path component of
path([$new_path]) Gets and sets the same value as
path_query([$path_here]) Sets and gets the escapted path and query components.
path_segments([$seg]) Sets and gets the path. In a scalar content,
port([$new_port]) Sets and gets the port, which is an integer. If
query([$q]) Sets and gets the escaped query component of
query_form([$key => $val]) Sets and gets query components that use the urlencoded format.
query_keywords([$keywords]) Sets and gets query components that use keywords separated by a
rel($base_uri) Returns a relative URI reference, if one exists. Otherwise,
scheme([$some_scheme]) Sets and gets the scheme part of the URI. Such values include:
userinfo([$new_userinfo]) Sets and gets the escaped "userinfo" part of the authority component (of the URI). Often, the userinfo will appear as a username and password separated by a colon. Bear in mind that sending a password in the clear is a bad idea. URI::EscapeThis module escapes or unescapes "unsafe" characters within a URL string. Unsafe characters in URLs are described by RFC 1738. Before you form URI::URL objects and use that class's methods, you should make sure your strings are properly escaped. This module does not create its own objects; it exports the following functions:
uri_escape uri, [regexp] Given a URI as the first parameter, returns the equivalent URI with certain characters replaced with $escaped_uri = uri_escape($uri, 'aeiou') This code escapes all lowercase vowels in
uri_unescape uri Substitutes any instance of URI::URLThis module creates URL objects that store all the elements of a URL. These objects are used by the The $url = URI::URL->new($url_string [, $base_url]) This method creates a new URI::URL object with the URL given as the first parameter. An optional base URL can be specified as the second parameter and is useful for generating an absolute URL from a relative URL. The following methods are for the URI::URL class.
$url->abs([base, [scheme]]) Returns the absolute URL, given a base. If invoked with no parameters, any previous definition of the base is used. The second parameter is a Boolean that modifies
$url->as_string( ) Returns the URL as a scalar string. All defined components of the URL are included in the string.
$url->base([base]) Gets or sets the base URL associated with the URL in this URI::URL object. The base URL is useful for converting a relative URL into an absolute URL.
$url->crack( ) Returns an array with the following data: (
$url->default_port([port]) When invoked with no parameters, this method returns the default port for the URL defined in the object. The default port is based on the scheme used. Even if the port for the URL is explicitly changed by the user with the
$url->eparams([param]) When invoked with no arguments, this method returns the escaped parameter of the URL defined in the object. When invoked with an argument, the object's escaped parameter is assigned to that value.
$url->epath( ) When invoked with no parameters, this method returns the escaped path of the URL defined in the object. When invoked with a parameter, the object's escaped path is assigned to that value.
$url->eq(other_url) Returns true when the object's URL is equal to the URL specified.
$url->equery([string]) When invoked with no arguments, this method returns the escaped query string of the URL defined in the object. When invoked with an argument, the object's escaped query string is assigned to that value.
$url->frag([frag]) When invoked with no arguments, this method returns the fragment of the URL defined in the object. When invoked with an argument, the object's fragment is assigned to that value.
$url->full_path( ) Returns a string consisting of the escaped path, escaped parameters, and escaped query string.
$url->host([hostname]) When invoked with no parameters, this method returns the hostname in the URL defined in the object. When invoked with a parameter, the object's hostname is assigned to that value.
$url->netloc([netloc]) When invoked with no parameters, this method returns the network location for the URL defined in the object. The network location is a string composed of "user:password@host:port", in which the user, password, and port may be omitted when not defined. When
$url->params([param]) Same as
$url->password([passwd]) When invoked with no parameters, this method returns the password in the URL defined in the object. When invoked with a parameter, the object's password is assigned to that value.
$url->path([pathname]) Same as
$url->port([port]) When invoked with no parameters, this method returns the port for the URL defined in the object. If a port wasn't explicitly defined in the URL, a default port is assumed. When invoked with a parameter, the object's port is assigned to that value.
$url->query([param]) Same as
$url->rel(base) Given a base as a first parameter or a previous definition of the base, returns the current object's URL relative to the base URL.
$url->scheme([scheme]) When invoked with no parameters, this method returns the scheme in the URL defined in the object. When invoked with a parameter, the object's scheme is assigned to that value.
URI::URL::strict(bool) When set, the URI::URL module calls
$url->user([username]) When invoked with no parameters, this method returns the user for the URL defined in the object. When invoked with a parameter, the object's user is assigned to that value. |