summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Changes15
-rw-r--r--MANIFEST7
-rw-r--r--MANIFEST.SKIP5
-rw-r--r--META.yml21
-rw-r--r--Makefile.PL8
-rw-r--r--lib/Number/Compare.pm99
-rw-r--r--t/Number-Compare.t50
7 files changed, 205 insertions, 0 deletions
diff --git a/Changes b/Changes
new file mode 100644
index 0000000..2fea84a
--- /dev/null
+++ b/Changes
@@ -0,0 +1,15 @@
+0.03 21st September, 2011
+ Generate a new tarball on linux. Doing the C<make dist> on OSX
+ generated a tarball that upset dumb tars
+ tar: Ignoring unknown extended header keyword `SCHILY.dev'
+ tar: Ignoring unknown extended header keyword `SCHILY.ino'
+ tar: Ignoring unknown extended header keyword `SCHILY.nlink'
+
+
+0.02 12th September, 2011
+ - apply warning-related fix -
+ https://rt.cpan.org/Public/Bug/Display.html?id=58466
+
+0.01 23rd October, 2002
+ - Refactored the code away from File::Find::Rule
+ - Initial release
diff --git a/MANIFEST b/MANIFEST
new file mode 100644
index 0000000..7719f77
--- /dev/null
+++ b/MANIFEST
@@ -0,0 +1,7 @@
+Changes
+MANIFEST
+MANIFEST.SKIP
+Makefile.PL
+t/Number-Compare.t
+lib/Number/Compare.pm
+META.yml Module meta-data (added by MakeMaker)
diff --git a/MANIFEST.SKIP b/MANIFEST.SKIP
new file mode 100644
index 0000000..461bdeb
--- /dev/null
+++ b/MANIFEST.SKIP
@@ -0,0 +1,5 @@
+\.svn
+\.cvsignore
+blib
+pm_to_blib
+Makefile
diff --git a/META.yml b/META.yml
new file mode 100644
index 0000000..967e6fd
--- /dev/null
+++ b/META.yml
@@ -0,0 +1,21 @@
+--- #YAML:1.0
+name: Number-Compare
+version: 0.03
+abstract: ~
+author: []
+license: unknown
+distribution_type: module
+configure_requires:
+ ExtUtils::MakeMaker: 0
+build_requires:
+ ExtUtils::MakeMaker: 0
+requires:
+ Test::More: 0
+no_index:
+ directory:
+ - t
+ - inc
+generated_by: ExtUtils::MakeMaker version 6.57_05
+meta-spec:
+ url: http://module-build.sourceforge.net/META-spec-v1.4.html
+ version: 1.4
diff --git a/Makefile.PL b/Makefile.PL
new file mode 100644
index 0000000..38ad682
--- /dev/null
+++ b/Makefile.PL
@@ -0,0 +1,8 @@
+use ExtUtils::MakeMaker;
+WriteMakefile(
+ NAME => 'Number::Compare',
+ VERSION_FROM => 'lib/Number/Compare.pm',
+ PREREQ_PM => {
+ 'Test::More' => 0,
+ },
+);
diff --git a/lib/Number/Compare.pm b/lib/Number/Compare.pm
new file mode 100644
index 0000000..36fc62f
--- /dev/null
+++ b/lib/Number/Compare.pm
@@ -0,0 +1,99 @@
+package Number::Compare;
+use strict;
+use Carp qw(croak);
+use vars qw/$VERSION/;
+$VERSION = '0.03';
+
+sub new {
+ my $referent = shift;
+ my $class = ref $referent || $referent;
+ my $expr = $class->parse_to_perl( shift );
+
+ bless eval "sub { \$_[0] $expr }", $class;
+}
+
+sub parse_to_perl {
+ shift;
+ my $test = shift;
+
+ $test =~ m{^
+ ([<>]=?)? # comparison
+ (.*?) # value
+ ([kmg]i?)? # magnitude
+ $}ix
+ or croak "don't understand '$test' as a test";
+
+ my $comparison = $1 || '==';
+ my $target = $2;
+ my $magnitude = $3 || '';
+ $target *= 1000 if lc $magnitude eq 'k';
+ $target *= 1024 if lc $magnitude eq 'ki';
+ $target *= 1000000 if lc $magnitude eq 'm';
+ $target *= 1024*1024 if lc $magnitude eq 'mi';
+ $target *= 1000000000 if lc $magnitude eq 'g';
+ $target *= 1024*1024*1024 if lc $magnitude eq 'gi';
+
+ return "$comparison $target";
+}
+
+sub test { $_[0]->( $_[1] ) }
+
+1;
+
+__END__
+
+=head1 NAME
+
+Number::Compare - numeric comparisons
+
+=head1 SYNOPSIS
+
+ Number::Compare->new(">1Ki")->test(1025); # is 1025 > 1024
+
+ my $c = Number::Compare->new(">1M");
+ $c->(1_200_000); # slightly terser invocation
+
+=head1 DESCRIPTION
+
+Number::Compare compiles a simple comparison to an anonymous
+subroutine, which you can call with a value to be tested again.
+
+Now this would be very pointless, if Number::Compare didn't understand
+magnitudes.
+
+The target value may use magnitudes of kilobytes (C<k>, C<ki>),
+megabytes (C<m>, C<mi>), or gigabytes (C<g>, C<gi>). Those suffixed
+with an C<i> use the appropriate 2**n version in accordance with the
+IEC standard: http://physics.nist.gov/cuu/Units/binary.html
+
+=head1 METHODS
+
+=head2 ->new( $test )
+
+Returns a new object that compares the specified test.
+
+=head2 ->test( $value )
+
+A longhanded version of $compare->( $value ). Predates blessed
+subroutine reference implementation.
+
+=head2 ->parse_to_perl( $test )
+
+Returns a perl code fragment equivalent to the test.
+
+=head1 AUTHOR
+
+Richard Clamp <richardc@unixbeard.net>
+
+=head1 COPYRIGHT
+
+Copyright (C) 2002,2011 Richard Clamp. All Rights Reserved.
+
+This module is free software; you can redistribute it and/or modify it
+under the same terms as Perl itself.
+
+=head1 SEE ALSO
+
+http://physics.nist.gov/cuu/Units/binary.html
+
+=cut
diff --git a/t/Number-Compare.t b/t/Number-Compare.t
new file mode 100644
index 0000000..0362504
--- /dev/null
+++ b/t/Number-Compare.t
@@ -0,0 +1,50 @@
+#!perl -w
+# $Id$
+use strict;
+use Test::More tests => 24;
+
+BEGIN { use_ok("Number::Compare") };
+
+my $c = Number::Compare->new('>20');
+ok( $c->test(21), ">20" );
+ok( !$c->test(20) );
+ok( !$c->test(19) );
+
+$c = Number::Compare->new('<20');
+ok( !$c->test(21), "<20" );
+ok( !$c->test(20) );
+ok( $c->test(19) );
+
+$c = Number::Compare->new('>=20');
+ok( $c->test(21), ">=20" );
+ok( $c->test(20) );
+ok( !$c->test(19) );
+
+$c = Number::Compare->new('<=20');
+ok( !$c->test(21), "<=20" );
+ok( $c->test(20) );
+ok( $c->test(19) );
+
+$c = Number::Compare->new('20');
+ok( !$c->test(21), "== 20" );
+ok( $c->test(20) );
+ok( !$c->test(19) );
+
+# well that's all the comparisons done, we'll not repeat that for each
+# of the magnitudes though
+
+ok( Number::Compare->new("2K")->test( 2_000), "K" );
+ok( Number::Compare->new("2M")->test( 2_000_000), "M" );
+ok( Number::Compare->new("2G")->test(2_000_000_000), "G" );
+
+ok( Number::Compare->new("2Ki")->test( 2_048), "Ki" );
+ok( Number::Compare->new("2Mi")->test( 2_097_152), "Mi" );
+ok( Number::Compare->new("2Gi")->test(2_147_483_648), "Gi" );
+
+# okay, how about if we become a blessed coderef
+
+ok( Number::Compare->new("1Ki")->(1024), "directly call the coderef" );
+
+# expose parse_to_perl
+
+is( Number::Compare->parse_to_perl(">1Ki"), '> 1024', "->parse_to_perl" );