Perl::Compare - Runtime access to perl comparison operators
use Perl::Compare qw(compare); print compare('eq', 'abc', 'xyz') ? "True\n" : "False\n"; use Perl::Compare qw(sort_compare); my @items = qw(The black cat climbed the green tree); sort { &sort_compare('cmp', $a, $b) } @items;
Nothing exported by default
compare, sort_compare, sort_keydepth
compare, sort_compare, sort_keydepth
This module requires these other modules and libraries:
Exporter
Efficient routine to compare scalar values when the operator is variable. This is particularly useful for dynamic patterns, such as those read in from a configuration file, taken from an argument, or specified in a table somewhere.
Efficiency and security, invoking eval (as eval
or m//e
) is not an
option.
In some situations, the logic is more important than the function. Just like
when you would use no warnings
. For this reason these routines simply
return the logical answer when values are undefined.
Basic functionality which can be extended. Okay, it's just nice to have some sugar some times. Extending this package with your own comparison routines should be straight foward. A few that we have added are:
mod Modulo bw Stringwise begins-with ew Stringwise ends-with
Because one cannot pass modifiers to the regular expression engine, the following extensions were added:
=~i Regular expression match, case insensitive !~i Regular expression does not match, case insensitive eqic Equal ignoring case neic Not equal ignoring case
The key is the comparison operator. The value is the subroutine which is executed.
If you wanted to define an operator named opr
, you would:
Perl::Compare::COMPARISONS{'opr'} = sub { # your code here };
Your comparison is invoked by:
compare('opr', 'a', 'b');
And your subroutine will be passed 'a'
and 'b'
. In fact, all arguments
are passed, allowing you to write tertiary comparisons, such as:
compare('opr', 'a', 'b', 'c');
Support runtime comparison when the operator is held as a scalar.
Where $comparator
may be one of:
&& $a and $b are true vlaues || $a or $b is a true value eq Stringwise equal ne Stringwise not equal lt Stringwise less than le Stringwise less than or equal gt Stringwise greater than ge Stringwise greater than or equal =~ Regular expression match !~ Regular expression does not match cmp Stringwise less-than, equal-to, or greater-than leg Alias for cmp (see Perl 6) == Numeric equal != Numeric not equal < Numeric less than > Numeric greater than <= Numeric less than or equal >= Numeric greater than or equal <=> Numeric less-than, equal-to, or greater-than =~i Regular expression match, case insensitive !~i Regular expression does not match, case insensitive eqic Equal ignoring case neic Not equal ignoring case mod Modulo bw Stringwise begins-with ew Stringwise ends-with
This example will return false:
compare('eq','',undef);
This example will return true:
compare('eq','abc','abc');
This example will return true:
compare('ne','abc','Abc');
This example will return false:
compare('eq','abc',undef);
This example will return true:
compare('!~','abc','A');
This example will return true:
compare('=~','abc','a');
This example will return true:
compare('==',1234,1234);
This example will return true:
compare('>=',1234,1234);
This example will return true:
compare('eqic','abc','Abc');
This example will return true:
compare('==',undef,undef);
This example will return false:
compare('==',0,undef);
This example will return false:
compare('!~i','abc','A');
This example will return true:
compare('=~i','abc','A');
This example will return true:
compare('eqic','abc','Abc');
This example will return false:
compare('neic','abc','Abc');
This example will return true:
compare('mod',4,2);
This example will return true:
compare('bw','abc','a');
This example will return false:
compare('bw','abc','b');
This example will return false:
compare('ew','abc','b');
This example will return true:
compare('ew','abc','c');
See also compare
This example:
my @numbers = ( 20, 1, 10, 2 ); join ';', sort { &sort_compare('<=>',$a,$b) } @numbers;
will return:
1;2;10;20
The key is the solodus (/) character.
This example will not abort:
use Perl::Compare qw(sort_keydepth);
This example:
# The deepest elements come last join ';', sort {&sort_keydepth($a, $b)} qw(t/w/o o/ne th/r/e/e);
will return:
o/ne;t/w/o;th/r/e/e
This example:
# Those without come after undefined but before those with no warnings 'uninitialized'; join ';', sort {&sort_keydepth($a, $b)} (qw(t/w/o o/ne none th/r/e/e), undef);
will return:
;none;o/ne;t/w/o;th/r/e/e
Ryan Gies <ryangies@cpan.org>
Copyright (C) 2014-2016 by Ryan Gies. All rights reserved. Copyright (C) 2006-2013 by Livesite Networks, LLC. All rights reserved. Copyright (C) 2000-2005 by Ryan Gies. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. * Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. * The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. To the best of our knowledge, no patented algorithms have been used. However, we do not have the resources to carry out a patent search, and therefore cannot give any guarantee of the above statement.