Build a uri from a set of named parameters
version 0.04
use URI::FromHash qw( uri ); my $uri = uri( path => '/some/path', query => { foo => 1, bar => 2 }, );
This module provides a simple one-subroutine \*(L"named parameters\*(R" style interface for creating URIs. Underneath the hood it uses \*(C`URI.pm\*(C', though because of the simplified interface it may not support all possible options for all types of URIs.
It was created for the common case where you simply want to have a simple interface for creating syntactically correct URIs from known components (like a path and query string). Doing this using the native \*(C`URI.pm\*(C' interface is rather tedious, requiring a number of method calls, which is particularly ugly when done inside a templating system such as Mason or \s-1TT2\s0.
This module provides two functions both of which are optionally exportable:
Both of these functions accept the same set of parameters, except for one additional parameter allowed when calling \*(C`uri()\*(C'.
The \*(C`uri()\*(C' function simply returns a string representing a canonicalized \s-1URI\s0 based on the provided parameters. The \*(C`uri_object()\*(C' function returns new a \*(C`URI.pm\*(C' object based on the given parameters.
These parameters are:
scheme The \s-1URI\s0's scheme. This is optional, and if none is given you will create a schemeless \s-1URI\s0. This is useful if you want to create a \s-1URI\s0 to a path on the same server (as is commonly done in \*(C`<a>\*(C' tags).
host
port
path The path can be either a string or an array reference. If an array reference is passed each defined member of the array will be joined by a single forward slash (/). If you are building a host-less \s-1URI\s0 and want to include a leading slash then make the first element of the array reference an empty string (''). You can add a trailing slash by making the last element of the array reference an empty string.
username
password
fragment All of these are optional strings which can be used to specify that part of the \s-1URI\s0.
query This should be a hash reference of query parameters. The values for each key may be a scalar or array reference. Use an array reference to provide multiple values for one key.
query_separator This option is can only be provided when calling \*(C`uri()\*(C'. By default, it is a semi-colon (;).
Please report any bugs or feature requests to \*(C`[email protected]\*(C', or through the web interface at <http://rt.cpan.org>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
Dave Rolsky <[email protected]>
This software is Copyright (c) 2013 by Dave Rolsky.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)