|
NAMEData::Cmp - Compare two data structures, return -1/0/1 like cmp VERSIONThis document describes version 0.010 of Data::Cmp (from Perl distribution Data-Cmp), released on 2021-04-12. SYNOPSIS use Data::Cmp qw(cmp_data);
cmp_data(["one", "two", "three"],
["one", "two", "three"]); # => 0
cmp_data(["one", "two" , "three"],
["one", "two2", "three"]); # => -1
cmp_data(["one", "two", "three"],
["one", "TWO", "three"]); # => 1
# hash/array is not "comparable" with scalar
cmp_data(["one", "two", {}],
["one", "two", "three"]); # => 2
Sort data structures (of similar structures): my @arrays = (["c"], ["b"], ["a", "b"], ["a"], ["a","c"]);
my @sorted = sort { cmp_data($a, $b) } @arrays; # => (["a"], ["a","b"], ["a","c"], ["b"], ["c"])
DESCRIPTIONThis relatively lightweight (no non-core dependencies, under 100 lines of code) module offers the "cmp_data" function that, like Perl's "cmp", returns -1/0/1 value. "cmp_data" differs from "cmp" in that it can compare two data of different types and compare data items recursively, with pretty sensible semantics. In addition to returning -1/0/1, "cmp_data" can also return 2 if two data differ but not comparable: there is no sensible notion of which one is "greater than" the other. An example is empty hash "{}" vs empty array "[]"). This module can handle circular structure. The following are the rules of comparison used by cmp_data():
FUNCTIONScmp_dataUsage: cmp_data($d1, $d2) => -1/0/1/2 HOMEPAGEPlease visit the project's homepage at <https://metacpan.org/release/Data-Cmp>. SOURCESource repository is at <https://github.com/perlancar/perl-Data-Cmp>. BUGSPlease report any bugs or feature requests on the bugtracker website <https://github.com/perlancar/perl-Data-Cmp/issues> 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. SEE ALSOData comparisonOther variants of Data::Cmp: Data::Cmp::Numeric, Data::Cmp::StrOrNumeric, Data::Cmp::Custom (allows custom actions and comparison routines), Data::Cmp::Diff (generates diff structure instead of just returning -1/0/1/2), Data::Cmp::Diff::Perl (generates diff in the form of Perl code). Modules that just return boolean result ("same or different"): Data::Compare, Test::Deep::NoTest (offers flexibility or approximate or custom comparison). Modules that return some kind of "diff" data: Data::Comparator, Data::Diff. Of course, to check whether two structures are the same you can also serialize each one then compare the serialized strings/bytes. There are many modules for serialization: JSON, YAML, Sereal, Data::Dumper, Storable, Data::Dmp, just to name a few. Test modules that do data structure comparison: Test::DataCmp (test module based on Data::Cmp::Custom), Test::More (is_deeply()), Test::Deep, Test2::Tools::Compare. OthersScalar::Cmp which employs roughly the same rules as Data::Cmp but does not recurse into arrays/hashes and is meant to compare two scalar values. AUTHORperlancar <perlancar@cpan.org> COPYRIGHT AND LICENSEThis software is copyright (c) 2021, 2019, 2018 by 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.
|