Scalar::Cmp - Compare two scalars
This document describes version 0.003 of Scalar::Cmp (from Perl distribution Scalar-Cmp), released on 2021-09-07.
use Scalar::Cmp qw(cmp_scalar cmpnum_scalar cmpstrornum_scalar); # undef say cmp_scalar(undef, undef); # => 0 say cmp_scalar(undef, 1); # => -1 # references say cmp_scalar(1, []); # => 2 say cmp_scalar([], 1); # => 2 say cmp_scalar([], []); # => 2 my $r = []; say cmp_scalar($r, $r); # => 0 # cmp_scalar always uses cmp (mnemonic: "cmp" operator) say cmpstr_scalar("1.0", 1); # => 1 # cmpnum_scalar always uses <=> say cmpnum_scalar("1.0", 1); # => 0 say cmpnum_scalar("a", "0"); # => 0, but emit warnings # cmpstrornum_scalar uses <=> if both scalars look like number, or cmp otherwise say cmpstrornum_scalar(1, 1); # => 0 say cmpstrornum_scalar(1, 2); # => -1 say cmpstrornum_scalar(2, 1); # => -1 say cmpstrornum_scalar("1.0", 1); # => 0 say cmpstrornum_scalar("a", "0"); # => 1
This module provides "cmp_scalar" (and "cmpnum_scalar" and "cmpstrornum_scalar" which are convenient routines to compare two scalar values (ii.e. check if they are the same, or find out who is "greater than" the other). The routines can handle undef and references, so you don't have to manually check for these.
undef
The routines return -1, 0, 1 like Perl's cmp and <=> operators, but also possibly 2 when the two scalars are different but there is no sensible notion of which one is larger than the other (e.g. 1 vs [1]). The following is the rule:
cmp
<=>
2
1
[1]
cmp_scalar(undef, 0); # => -1
cmp_scalar(undef, undef); # => 0
Note: This might not be what you want if you expect undef to act like NULL in relational databases, where NULL is not equal to itself.
NULL
cmp_scalar(1, []); # => 2 cmp_scalar([], 1); # => 2
cmp_scalar([], []); # => 2 my $ary = []; cmp_scalar($ary, $ary); # => 0, same "address"
"cmp_scalar" always uses cmp. "cmpnum_scalar" always uses <=>. "cmpstrornum_scalar" uses <=> if both scalars look like number, or cmp otherwise.
Please visit the project's homepage at https://metacpan.org/release/Scalar-Cmp.
Source repository is at https://github.com/perlancar/perl-Scalar-Cmp.
The Perl's cmp and <=> operators.
Data::Cmp which uses similar comparison rules but recurse into array and hash elements.
Syntax::Operator::Equ introduces several operators/functions which also have a rule to equate two undefs.
perlancar <perlancar@cpan.org>
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, and sometimes one or two other Dist::Zilla plugin and/or Pod::Weaver::Plugin. Any additional steps required beyond that are considered a bug and can be reported to me.
This software is copyright (c) 2021 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.
Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=Scalar-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.
To install Scalar::Cmp, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Scalar::Cmp
CPAN shell
perl -MCPAN -e shell install Scalar::Cmp
For more information on module installation, please visit the detailed CPAN module installation guide.