NAME
    Data::Cmp - Compare two data structures, return -1/0/1 like cmp

VERSION
    This document describes version 0.002 of Data::Cmp (from Perl
    distribution Data-Cmp), released on 2018-08-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):

     use Data::Cmp qw(cmp_data);
     use Data::Dump;
     my @arrays = ([3], [1], [-1, 2], [0,0], [1,2]);
     dd sort { cmp_data($a, $b) } @arrays;

DESCRIPTION
    This relatively compact module offers the "cmp_data" function that, like
    Perl's "cmp", returns -1/0/1 value. In addition to that, it can also
    return 2 if the two data structures differ but there is no sensible
    notion of which one is larger than the other.

    This module can handle circular structure.

    The following are the rules of comparison used by "cmp_data()":

    *   Two undefs are the same (0)

    *   A defined value is greater than (1) undef

    *   Two non-reference scalars are compared string-wise using Perl's cmp

    *   A reference and non-reference are different (2)

    *   Two references that are of different types are different (2)

    *   Blessed references that are blessed into different packages are
        different (2)

    *   Array references are compared element by element

    *   A longer array is greater than (1) its shorter subset

    *   Hash references are compared key by key

    *   When two hash references share a common subset of pairs, the greater
        is the one that has more non-common pairs

FUNCTIONS
  cmp_data
    Usage:

     cmp_data($d1, $d2) => -1/0/1/2

HOMEPAGE
    Please visit the project's homepage at
    <https://metacpan.org/release/Data-Cmp>.

SOURCE
    Source repository is at <https://github.com/perlancar/perl-Data-Cmp>.

BUGS
    Please report any bugs or feature requests on the bugtracker website
    <https://rt.cpan.org/Public/Dist/Display.html?Name=Data-Cmp>

    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 ALSO
    Other variants of Data::Cmp: "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.

AUTHOR
    perlancar <perlancar@cpan.org>

COPYRIGHT AND LICENSE
    This software is copyright (c) 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.

