Query::Param - Lightweight object interface for parsing and creating query strings and form parameters
use Query::Param;
my $args = Query::Param->new("foo=1&bar=2&bar=3&empty=&encoded=%25+%2B");
# Object-style access
my $foo = $args->get("foo"); # scalar: "1"
my $bar = $args->get("bar"); # arrayref: ["2", "3"]
my $encoded = $args->get("encoded"); # scalar: "% +"
# CGI-style access
my $foo_again = $args->param("foo"); # same as get("foo")
my @keys = $args->param; # all parameter names
# Get all decoded parameters
my $all = $args->params; # { foo => "1", bar => ["2", "3"], ... }
# Legacy-compatible flat hash
my $vars = $args->Vars; # { foo => "1", bar => "3", ... }
# Check for presence
if ( $args->has("bar") ) { ... }
# Update or add parameters
$args->set("foo", "updated");
$args->set("new", "value");
# Get query string back
my $str = $args->to_string; # bar=2&bar=3&empty=&encoded=%25%20%2B&foo=updated&new=value
This module parses an application/x-www-form-urlencoded encode query string and provides an object-oriented interface for accessing the query parameters.
Multiple values for a parameter are stored as an array
internally. When accessed via get, a scalar is returned for single
values, and an array reference for multiple values.
There are many modules that parse query strings, so why re-invent this wheel?
to_string() - values go in and come
back out encoded correctly, even if original encoding format differed
(+ vs %20).to_string() and new() are inverse operations, as
long as values are treated semantically.set() replaces; multiple values only come from the original
string or if assigned intentionally.URI::Escape, no other non-core deps.CGI::Tiny is good, but intentionally avoids mutation - no
set().Plack::Request and HTTP::Request::Params require full request
objects and more dependencies.This module supports key methods from CGI for interoperability:
param() - scalar or arrayref return, regardless of contextVars() - returns a hashref of flattened scalar values (last-value wins)get() - equivalent to param($key)params() - returns a hashref retaining all values (including
arrayrefs)to_string() - round-trips encoded input with full fidelityNote: Unlike CGI.pm, param() and get() do not change behavior
depending on context. They always return a scalar (if one value) or an
arrayref (if multiple values). This avoids subtle bugs and improves
predictability.
This module does not use any global state. It is safe to use in threaded, embedded, and reentrant environments such as mod_perl, Plack, or inside event loops.
my $args = Query::Param->new($query_string);
Parses the provided query string and returns a new
Query::Param object.
my $args = Query::Param->new_from_request;
Parses query strings, application/x-www-form-urlencoded, or multipart/form-data from HTTP requests. Assumes environment variables CONTENT_TYPE, CONTENT_LENGTH, REQUEST_METHOD have been set.
NOTE: Reminder - this is a lightweight parser! It does not support file downloads when data is passed as multipart/form-data.
If the content type is application/json the parser will decode the
payload. You can retrieve the raw payload using the to_string
method or individual keys of the payload using get().
$value = $args->get($key);
Returns the value associated with $key. If there are multiple
values, an array reference is returned. If only one value exists, the
scalar is returned. Returns undef if the key does not exist.
if ($args->has("foo")) { ... }
Returns true if the key exists in the query string. This method accesses the tied hash internally.
Returns the keys or names of the query string parameters.
Returns a list of array references that contain key/value pairs in the
same vein as List::Util::pairs.
my @names = $q->param;
my $value = $q->param('key');
Returns the list of all parameter names when called with no arguments.
When called with a key, returns the value for that parameter. If the parameter occurred multiple times in the original query string, returns an array reference of values. Otherwise, returns a scalar value.
This method is provided for compatibility with CGI-param>, but
unlike CGI.pm, it always returns a scalar or array reference
regardless of context. Internally, it delegates to get().
my $hashref = $q->params;
Returns a hash reference containing all decoded parameters.
Each key corresponds to a parameter name. The value is either a scalar (if the parameter had a single value) or an array reference (if the parameter occurred multiple times).
This method is intended as a replacement for CGI-Vars> and provides
a consistent view of all parameters for inspection, testing, or
export.
Sets a query string parameter.
Creates an query string from the parsed or set parameters.
my $vars = $q->Vars;
Returns a hash reference where each key maps to a scalar value.
If a parameter occurred multiple times in the query string, only the
last value is preserved - consistent with CGI-Vars>, but
potentially lossy.
This method is provided for compatibility with legacy code that
expects flattened query strings. Use params() instead to retain
full value lists and avoid silent data loss.
Rob Lauer - rlauer6@comcast.net
This module is released under the same terms as Perl itself.