NAME
Filename::KeyValue - Parse filename using the KeyValue naming scheme
VERSION
This document describes version 0.001 of Filename::KeyValue (from Perl distribution Filename-KeyValue), released on 2026-04-27.
SYNOPSIS
use Filename::KeyValue qw(
parse_keyvalue_filename
normalize_keyvalue_filename
);
DESCRIPTION
FUNCTIONS
parse_keyvalue_filename
Usage:
parse_keyvalue_filename(%args) -> [$status_code, $reason, $payload, \%result_meta]
Parse filename using the KeyValue naming scheme.
Examples:
A single key=value pair:
parse_keyvalue_filename(filename => "foo-bar-kw1=val1.jpg");Result:
[ 200, "OK", { ext => "jpg", kv => { kw1 => ["val1"] }, kv_raw => "kw1=val1", prefix => "foo-bar", }, {}, ]Two key=value pairs:
parse_keyvalue_filename(filename => "foo-bar-kw1=val1-kw2=val2.jpg");Result:
[ 200, "OK", { ext => "jpg", kv => { kw1 => ["val1"], kw2 => ["val2"] }, kv_raw => "kw1=val1-kw2=val2", prefix => "foo-bar", }, {}, ]A single key=value pair containing two values:
parse_keyvalue_filename(filename => "foo-bar-kw1=val1,val1b.jpg");Result:
[ 200, "OK", { ext => "jpg", kv => { kw1 => ["val1", "val1b"] }, kv_raw => "kw1=val1,val1b", prefix => "foo-bar", }, {}, ]Three key=value pairs, one containing multiple values:
parse_keyvalue_filename(filename => "foo-bar-kw1=val1-kw2=val2,val2b,val2c-kw3=val3.jpg");Result:
[ 200, "OK", { ext => "jpg", kv => { kw1 => ["val1"], kw2 => ["val2", "val2b", "val2c"], kw3 => ["val3"] }, kv_raw => "kw1=val1-kw2=val2,val2b,val2c-kw3=val3", prefix => "foo-bar", }, {}, ]Empty key=value pairs:
parse_keyvalue_filename(filename => "foo-bar-kw1=-kw2=-kw3=val.jpg");Result:
[ 200, "OK", { ext => "jpg", kv => { kw3 => ["val"] }, kv_raw => "kw1=-kw2=-kw3=val", prefix => "foo-bar", }, {}, ]A value containing dash:
parse_keyvalue_filename(filename => "foo-bar-kw1_containing_dash=containing%2ddash-kw2_also_containing_dash=containing%2Dtwo%2Ddashes.jpg");Result:
[ 200, "OK", { ext => "jpg", kv => { kw1_containing_dash => ["containing-dash"], kw2_also_containing_dash => ["containing-two-dashes"], }, kv_raw => "kw1_containing_dash=containing%2ddash-kw2_also_containing_dash=containing%2Dtwo%2Ddashes", prefix => "foo-bar", }, {}, ]A value containing comma:
parse_keyvalue_filename(filename => "foo-bar-kw1=containing%2ccomma.jpg");Result:
[ 200, "OK", { ext => "jpg", kv => { kw1 => ["containing", "comma"] }, kv_raw => "kw1=containing%2ccomma", prefix => "foo-bar", }, {}, ]
The KeyValue naming scheme puts key=value pairs at the end of filename. Filename must match this regex:
/\A
(?:
(.+?) # optional prefix (part before the first key)
-
)?
(
(?:
([A-Za-z_][A-Za_z0-9_]*) # key
=
([^-]*) # value
)
(?:
-
([A-Za-z_][A-Za_z0-9_]*)
=
([^-]*)
)*
(\.\w+)? # optional filename extension
\z/x
KeyValue naming scheme is used in the AssetView media assets organization scheme (see Media::AssetView).
This routine parses a filename and return a structure containing parsed elements.
This function is not exported by default, but exportable.
Arguments ('*' denotes required arguments):
array_value => int (default: 1)
Always/never/maybe return value as array.
The default (1) is to return a scalar when there is a single value, or an array if there are multiple values, for example:
foo-kw1=val1-kw2=val2,val2b-kw3=val3-kw1=val1b.jpgthen:
kw1 = ['val1', 'val1b'] kw2 = ['val2', 'val2b'] kw3 = 'val3'The setting 0 means to never return array, so will return a comma-separated string instead. However, if the value is URI-decoded then this can potentially be ambiguous:
kw1=val1,val2-kw1=val3%2cval4.jpgunder array_value=0 and decode_value=1 will return:
kw1 = 'val1,val2,val3,val4'while under array_value=1 or 2 will return 3 elements:
kw1 = ['val1', 'val2', 'val3,val4']decode_value => bool (default: 1)
Whether to decode value with URI-encoding.
filename* => str
(No description)
Returns an enveloped result (an array).
First element ($status_code) is an integer containing HTTP-like status code (200 means OK, 4xx caller error, 5xx function error). Second element ($reason) is a string containing error message, or something like "OK" if status is 200. Third element ($payload) is the actual result, but usually not present when enveloped result is an error response ($status_code is not 2xx). Fourth element (%result_meta) is called result metadata and is optional, a hash that contains extra information, much like how HTTP response headers provide additional metadata.
Return value: (any)
HOMEPAGE
Please visit the project's homepage at https://metacpan.org/release/Filename-KeyValue.
SOURCE
Source repository is at https://github.com/perlancar/perl-Filename-KeyValue.
SEE ALSO
AUTHOR
perlancar <perlancar@cpan.org>
CONTRIBUTING
To contribute, you can send patches by email/via RT, or send pull requests on GitHub.
Most of the time, you don't need to build the distribution yourself. You can simply modify the code, then test via:
% prove -l
If you want to build the distribution (e.g. to try to install it locally on your system), you can install Dist::Zilla, Dist::Zilla::PluginBundle::Author::PERLANCAR, Pod::Weaver::PluginBundle::Author::PERLANCAR, and sometimes one or two other Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond that are considered a bug and can be reported to me.
COPYRIGHT AND LICENSE
This software is copyright (c) 2026 by perlancar <perlancar@cpan.org>.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
BUGS
Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=Filename-KeyValue
When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.