=head1 NAME

Perl::Comparison - Runtime access to perl comparison operators


=head1 SYNOPSIS


  use Perl::Comparison qw(compare);
  print compare('eq', 'abc', 'xyz') ? "True\n" : "False\n";
    
  use Perl::Comparison qw(sort_compare);
  my @items = qw(The black cat climbed the green tree);
  sort { &sort_compare('cmp', $a, $b) } @items;



=head1 EXPORTS

Nothing exported by default

=over

=item Exported upon request

compare, sort_compare, sort_keydepth


=item :all

compare, sort_compare, sort_keydepth


=back

=head1 DEPENDENCIES

This module requires these other modules and libraries:

    Exporter

=head1 DESCRIPTION


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.

=head2 Motivation

Efficiency and security, invoking eval (as C<eval> or C<m//e>) is not an 
option.

In some situations, the logic is more important than the function.  Just like
when you would use C<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

=head2 Notes

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




=head1 PUBLIC INTERFACE

=head2 %COMPARISIONS

=over

=item Comparison routines



The key is the comparison operator. The value is the subroutine which is 
executed.

If you wanted to define an operator named C<opr>, you would:


  Perl::Comparison::COMPARISONS{'opr'} = sub {
    # your code here
  };

Your comparison is invoked by:


  compare('opr', 'a', 'b');

And your subroutine will be passed C<'a'> and C<'b'>.  In fact, all arguments
are passed, allowing you to write tertiary comparisons, such as:


  compare('opr', 'a', 'b', 'c');



=back


=head2 compare

=over

=item Wrapper for Perl's internal comparison operators.




Support runtime comparison when the operator is held as a scalar.

Where C<$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');




=back


=head2 sort_compare

=over

=item Compare for sorting, returning 1, 0 or -1



See also L</compare>


This example:


  my @numbers = ( 20, 1, 10, 2 );
  join ';', sort { &sort_compare('<=>',$a,$b) } @numbers;



I<will return>:

  1;2;10;20



=back


=head2 sort_keydepth

=over

=item Sort by the number of nodes in the key



The key is the solodus (/) character.




This example will B<not> abort:


  use Perl::Comparison 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);



I<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);



I<will return>:

  ;none;o/ne;t/w/o;th/r/e/e



=back


=head1 PACKAGE INTERNALS

=head2 _either_undef

=over

=item Are either operands undefined?






=back


=head2 _both_undef

=over

=item Are both operands undefined?






=back



=head1 AUTHORS

    Ryan Gies <ryangies@cpan.org>

=head1 COPYRIGHT

    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.

=cut

