NAME
Comparer - Reusable comparer subroutines
SPECIFICATION VERSION
0.1
VERSION
This document describes version 0.1.1 of Comparer (from Perl
distribution Comparer), released on 2024-01-23.
SYNOPSIS
Basic:
use Comparer::naturally;
my $cmp = Comparer::naturally::gen_comparer;
my @sorted = sort { $cmp->($a,$b) } ('track1.mp3', 'track10.mp3', 'track2.mp3', 'track1b.mp3', 'track1a.mp3');
# => ('track1.mp3', 'track1a.mp3', 'track1b.mp3', 'track2.mp3', 'track10.mp3')
Specifying arguments:
use Comparer::naturally;
my $cmp = Comparer::naturally::gen_comparer(reverse => 1);
my @sorted = sort { $cmp->($a,$b) } ...;
Specifying comparer on the command-line (for other CLI's):
% customsort -c naturally ...
% customsort -c naturally=reverse,1 ...
DESCRIPTION
EXPERIMENTAL. SPEC MIGHT STILL CHANGE.
Glossary
A comparer is a subroutine that accepts two items to be compare and
return a value of either -1/0/1. So in other words, just like Perl's
"cmp" or "<=>".
A comparer module is a module under the "Comparer::*" namespace that you
can use to generate a comparer.
Writing a Comparer module
package Comparer::naturally;
# required. must return a hash (DefHash)
sub meta {
return +{
v => 1,
args => {
reverse => {
schema => 'bool*', # Sah schema
},
},
};
}
sub gen_comparer {
my %args = @_;
...
}
1;
Namespace organization
HOMEPAGE
Please visit the project's homepage at
<https://metacpan.org/release/Comparer>.
SOURCE
Source repository is at <https://github.com/perlancar/perl-Comparer>.
SEE ALSO
Base specifications
DefHash
Sah
Related specifications
Sorter
SortKey
Previous incarnation
Sort::Sub
"Sorter", "SortKey", and "Comparer" are meant to eventually supersede
Sort::Sub. The main improvement over Sort::Sub is the split into three
kinds of subroutines:
1. sorter
A subroutine that accepts a list of items to sort.
"Sorter::*" modules are meant to generate sorters.
2. sort key generator
A subroutine that converts an item to a string/numeric key suitable
for simple comparison using "eq" or "==" during sorting.
"SortKey::*" modules are meant to generate sort key generators.
3. comparer
A subroutine that compares two items. Can be used in sort() as
custom sort block.
"Comparer::*" modules are meant to generate comparers.
Perl's sort(), as mentioned above, allows us to specify a comparer,
but oftentimes it's more efficient to sort by key using key
generator, where the keys are often cached. And sometimes due to
preprocessing and/or postprocessing it's more suitable to use the
more generic sorter interface.
Aside from the above, "Comparer" also makes Sort::Sub's special
arguments "reverse" and "is_ci" become ordinary arguments, because they
are not always applicable in all situation, especially "is_ci".
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) 2024 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=Comparer>
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.