From 69504c20852bc937767d54b0cbe32ad3a33f0055 Mon Sep 17 00:00:00 2001 From: ben hengst Date: Mon, 18 Mar 2013 22:43:33 -0700 Subject: [PATCH 01/11] building out two new rounding methods as is discussed in #227 --- lib/perl5i/2/SCALAR.pm | 14 +++++++++++++- t/number.t | 7 +++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/perl5i/2/SCALAR.pm b/lib/perl5i/2/SCALAR.pm index a09fac9..15c6a68 100644 --- a/lib/perl5i/2/SCALAR.pm +++ b/lib/perl5i/2/SCALAR.pm @@ -62,7 +62,6 @@ sub rtrim { sub trim { my $charset = $_[1]; - return rtrim(ltrim($_[0], $charset), $charset); } @@ -146,6 +145,19 @@ sub round { } } +sub round_to_nearest{ + my ($number,$nearest) = @_; + return ($number/$nearest)->round * $nearest; +} + +sub round_to_precision{ + my ($number,$precision) = @_; + # NOTE: is there ever going to be a reason to have a negative or + # real number here? Should this be an exception? + my $pre = '1'. '0'x int(abs($precision)); + return ($number*$pre)->round / $pre; +} + require Scalar::Util; *is_number = \&Scalar::Util::looks_like_number; sub is_positive { $_[0]->is_number && $_[0] > 0 } diff --git a/t/number.t b/t/number.t index d155b95..acd5525 100644 --- a/t/number.t +++ b/t/number.t @@ -23,6 +23,13 @@ is( (-3.51)->round, -4 ); is( (-3.5)->round, -4 ); is( (-3.49)->round, -3 ); +is( 13->round_to_nearest(5), 15); +is( 1.23->round_to_nearest(.05), 1.25); + +is( 5.6255->round_to_precision(3), 5.626); +is( 5.5->round_to_precision(10), 5.5); +is( 5.5->round_to_precision(-10.3), 5.5); # only uses abs int: no warnings + ok( 12->is_number ); ok(!'FF'->is_number ); From 0a166634016c43b930996abf97977a9f688b7c53 Mon Sep 17 00:00:00 2001 From: ben hengst Date: Wed, 20 Mar 2013 12:33:19 -0700 Subject: [PATCH 02/11] making round_to_precision bail when handed bad input for $precision --- lib/perl5i/2/SCALAR.pm | 7 ++++--- t/number.t | 3 ++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/perl5i/2/SCALAR.pm b/lib/perl5i/2/SCALAR.pm index 15c6a68..65b3dfe 100644 --- a/lib/perl5i/2/SCALAR.pm +++ b/lib/perl5i/2/SCALAR.pm @@ -152,9 +152,10 @@ sub round_to_nearest{ sub round_to_precision{ my ($number,$precision) = @_; - # NOTE: is there ever going to be a reason to have a negative or - # real number here? Should this be an exception? - my $pre = '1'. '0'x int(abs($precision)); + my $used_precision = int(abs($precision)); + Carp::Fix::1_25::croak('round_to_precision does not support non-positive non-integer values.') + unless $used_precision == $precision; + my $pre = '1'. '0'x $used_precision; return ($number*$pre)->round / $pre; } diff --git a/t/number.t b/t/number.t index acd5525..782ab53 100644 --- a/t/number.t +++ b/t/number.t @@ -28,7 +28,8 @@ is( 1.23->round_to_nearest(.05), 1.25); is( 5.6255->round_to_precision(3), 5.626); is( 5.5->round_to_precision(10), 5.5); -is( 5.5->round_to_precision(-10.3), 5.5); # only uses abs int: no warnings +eval{5.5->round_to_precision(-10.3)}; +ok( $@ =~ m/^round_to_precision does not support non-positive non-integer values/); ok( 12->is_number ); ok(!'FF'->is_number ); From 713bac5aacfe9b19d5ce825902be928845b1a99a Mon Sep 17 00:00:00 2001 From: ben hengst Date: Wed, 20 Mar 2013 12:40:11 -0700 Subject: [PATCH 03/11] adding documentation for round_to_nearest and round_to_precision --- lib/perl5i.pm | 19 +++++++++++++++++++ t/number.t | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/perl5i.pm b/lib/perl5i.pm index b933cc4..aa7737d 100644 --- a/lib/perl5i.pm +++ b/lib/perl5i.pm @@ -357,6 +357,25 @@ Rounds the $number towards negative infinity. floor() is a synonyn for round_down(). +=head3 round_to_nearest + + my $new_number = $number->round_to_nearest($nearest); + +Rounds the $number to the closest $nearest. + + 13->round_to_nearest(5); # 15 + 1.23->round_to_nearest(.05); # 1.25 + +=head3 round_to_precision + + my $new_number = $number->round_to_precision($precision); + +Rounds $number to $precision digits past the decimal point. The value of +$precision is expected to be a positive integer, any other value will +result in an exception. + + 5.625512332->round_to_precision(3); # 5.626 + 5.5->round_to_precision(10); # 5.5 =head3 is_number diff --git a/t/number.t b/t/number.t index 782ab53..200a8fa 100644 --- a/t/number.t +++ b/t/number.t @@ -26,7 +26,7 @@ is( (-3.49)->round, -3 ); is( 13->round_to_nearest(5), 15); is( 1.23->round_to_nearest(.05), 1.25); -is( 5.6255->round_to_precision(3), 5.626); +is( 5.625512332->round_to_precision(3), 5.626); is( 5.5->round_to_precision(10), 5.5); eval{5.5->round_to_precision(-10.3)}; ok( $@ =~ m/^round_to_precision does not support non-positive non-integer values/); From f25c9e062948c381ba8835e88ce8ee040412007d Mon Sep 17 00:00:00 2001 From: ben hengst Date: Wed, 20 Mar 2013 19:01:42 -0700 Subject: [PATCH 04/11] making the testing changes as requested by @schwern --- t/number.t | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/t/number.t b/t/number.t index 200a8fa..c7ca5cc 100644 --- a/t/number.t +++ b/t/number.t @@ -28,8 +28,8 @@ is( 1.23->round_to_nearest(.05), 1.25); is( 5.625512332->round_to_precision(3), 5.626); is( 5.5->round_to_precision(10), 5.5); -eval{5.5->round_to_precision(-10.3)}; -ok( $@ =~ m/^round_to_precision does not support non-positive non-integer values/); +ok eval{5.5->round_to_precision(-10.3)}; +like( $@, m/^round_to_precision does not support non-positive non-integer values/); ok( 12->is_number ); ok(!'FF'->is_number ); From 808f007aa54c0376fd667bfbb3ee082b32e0a33a Mon Sep 17 00:00:00 2001 From: "Michael G. Schwern" Date: Fri, 19 Jun 2009 22:04:09 -0700 Subject: [PATCH 05/11] Add IO::All but chop out the guessing about what it is. Only work with files and leave everything else to be explicit. Also add io->url() for opening URLs. --- lib/perl5i/IO.pm | 82 ++++++++++++++++++++++++++++++++++++++++++++++++ t/io-url.t | 29 +++++++++++++++++ t/io.t | 13 ++++++++ 3 files changed, 124 insertions(+) create mode 100644 lib/perl5i/IO.pm create mode 100644 t/io-url.t create mode 100644 t/io.t diff --git a/lib/perl5i/IO.pm b/lib/perl5i/IO.pm new file mode 100644 index 0000000..42eec51 --- /dev/null +++ b/lib/perl5i/IO.pm @@ -0,0 +1,82 @@ +package perl5i::IO; + +use strict; +use warnings; + +use IO::All; +use IO::All::LWP; + + +# This is IO::All->new but with most of the magic +# stripped out. +sub safer_io { + my $name = shift; + + # No name, return an empty io() object. + return io() unless defined $name; + + # Its an IO::All object, return it. + return $name if UNIVERSAL::isa($name, 'IO::All'); + + my $io = io(); + + # Its a filehandle + return $io->handle($name) + if UNIVERSAL::isa($name, 'GLOB') or ref(\ $name) eq 'GLOB'; + + # link is first because a link to a dir returns true for + # both -l and -d. + return $io->link($name) if -l $name; + return $io->file($name) if -f $name; + return $io->dir($name) if -d $name; + + # Maybe its a file they're going to write to later + $io->name($name); + return $io; +} + + +{ + package IO::All; + + use strict; + use warnings; + + sub url { + my $self = shift; + my $url = shift; + + my($method) = $url =~ /^([a-z]+):/; + $method ||= "http"; + $method = "file_url" if $method eq 'file'; + + # $self->can($scheme) or + # croak "url() does not know how to open scheme type $scheme"; + + return $self->$method($url); + } +} + + +{ + package IO::All::FILE_URL; + + # Convince IO::All that we exist + $INC{"IO/All/FILE_URL.pm"} = 1; + + use strict; + use warnings; + + use IO::All::LWP "-base"; + + const type => "file"; + + sub file_url { + my $self = shift; + + return $self->lwp_init(__PACKAGE__, @_); + } +} + + +1; diff --git a/t/io-url.t b/t/io-url.t new file mode 100644 index 0000000..b65fae0 --- /dev/null +++ b/t/io-url.t @@ -0,0 +1,29 @@ +#!/usr/bin/perl -w + +use perl5i; +use Test::More; +use Test::Exception; + +chdir 't'; + +# Test we can talk to a file URL +{ + "hello\nstuff\n" > io("io-url-test"); + ok -e "io-url-test"; + END { unlink "io-url-test" } + + is io->url("file://$CWD/io-url-test")->slurp, "hello\nstuff\n"; +} + + +# Test that we normally won't try to open a URL +{ + throws_ok { + io("http://www.google.com")->all; + } qr{^Can't open file 'http://www.google.com' for input}, + "io() won't open a URL as a URL"; +} + + +done_testing(); + diff --git a/t/io.t b/t/io.t new file mode 100644 index 0000000..d3601ce --- /dev/null +++ b/t/io.t @@ -0,0 +1,13 @@ +#!/usr/bin/perl -w + +use perl5i; +use Test::More; + +chdir 't'; + +"hello" > io("io-test"); +END { unlink "io-test" } + +is io("io-test")->slurp, "hello"; + +done_testing(); From a77254141b79acc6c0f839abbeb41408b18f2123 Mon Sep 17 00:00:00 2001 From: ben hengst Date: Mon, 18 Mar 2013 22:43:33 -0700 Subject: [PATCH 06/11] building out two new rounding methods as is discussed in #227 --- lib/perl5i/2/SCALAR.pm | 14 +++++++++++++- t/number.t | 7 +++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/perl5i/2/SCALAR.pm b/lib/perl5i/2/SCALAR.pm index 940bfec..ca8eae7 100644 --- a/lib/perl5i/2/SCALAR.pm +++ b/lib/perl5i/2/SCALAR.pm @@ -62,7 +62,6 @@ sub rtrim { sub trim { my $charset = $_[1]; - return rtrim(ltrim($_[0], $charset), $charset); } @@ -146,6 +145,19 @@ sub round { } } +sub round_to_nearest{ + my ($number,$nearest) = @_; + return ($number/$nearest)->round * $nearest; +} + +sub round_to_precision{ + my ($number,$precision) = @_; + # NOTE: is there ever going to be a reason to have a negative or + # real number here? Should this be an exception? + my $pre = '1'. '0'x int(abs($precision)); + return ($number*$pre)->round / $pre; +} + require Scalar::Util; *is_number = \&Scalar::Util::looks_like_number; sub is_positive { $_[0]->is_number && $_[0] > 0 } diff --git a/t/number.t b/t/number.t index d155b95..acd5525 100644 --- a/t/number.t +++ b/t/number.t @@ -23,6 +23,13 @@ is( (-3.51)->round, -4 ); is( (-3.5)->round, -4 ); is( (-3.49)->round, -3 ); +is( 13->round_to_nearest(5), 15); +is( 1.23->round_to_nearest(.05), 1.25); + +is( 5.6255->round_to_precision(3), 5.626); +is( 5.5->round_to_precision(10), 5.5); +is( 5.5->round_to_precision(-10.3), 5.5); # only uses abs int: no warnings + ok( 12->is_number ); ok(!'FF'->is_number ); From dfd293f4529bfa64a06c7248582f65ba91a2e2f4 Mon Sep 17 00:00:00 2001 From: ben hengst Date: Wed, 20 Mar 2013 12:33:19 -0700 Subject: [PATCH 07/11] making round_to_precision bail when handed bad input for $precision --- lib/perl5i/2/SCALAR.pm | 7 ++++--- t/number.t | 3 ++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/perl5i/2/SCALAR.pm b/lib/perl5i/2/SCALAR.pm index ca8eae7..5eb116a 100644 --- a/lib/perl5i/2/SCALAR.pm +++ b/lib/perl5i/2/SCALAR.pm @@ -152,9 +152,10 @@ sub round_to_nearest{ sub round_to_precision{ my ($number,$precision) = @_; - # NOTE: is there ever going to be a reason to have a negative or - # real number here? Should this be an exception? - my $pre = '1'. '0'x int(abs($precision)); + my $used_precision = int(abs($precision)); + Carp::Fix::1_25::croak('round_to_precision does not support non-positive non-integer values.') + unless $used_precision == $precision; + my $pre = '1'. '0'x $used_precision; return ($number*$pre)->round / $pre; } diff --git a/t/number.t b/t/number.t index acd5525..782ab53 100644 --- a/t/number.t +++ b/t/number.t @@ -28,7 +28,8 @@ is( 1.23->round_to_nearest(.05), 1.25); is( 5.6255->round_to_precision(3), 5.626); is( 5.5->round_to_precision(10), 5.5); -is( 5.5->round_to_precision(-10.3), 5.5); # only uses abs int: no warnings +eval{5.5->round_to_precision(-10.3)}; +ok( $@ =~ m/^round_to_precision does not support non-positive non-integer values/); ok( 12->is_number ); ok(!'FF'->is_number ); From f3103f230a67cc623e89093619b42cc615042a40 Mon Sep 17 00:00:00 2001 From: ben hengst Date: Wed, 20 Mar 2013 12:40:11 -0700 Subject: [PATCH 08/11] adding documentation for round_to_nearest and round_to_precision --- lib/perl5i.pm | 19 +++++++++++++++++++ t/number.t | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/perl5i.pm b/lib/perl5i.pm index 77a61bb..6b3782c 100644 --- a/lib/perl5i.pm +++ b/lib/perl5i.pm @@ -367,6 +367,25 @@ Rounds the $number towards negative infinity. floor() is a synonyn for round_down(). +=head3 round_to_nearest + + my $new_number = $number->round_to_nearest($nearest); + +Rounds the $number to the closest $nearest. + + 13->round_to_nearest(5); # 15 + 1.23->round_to_nearest(.05); # 1.25 + +=head3 round_to_precision + + my $new_number = $number->round_to_precision($precision); + +Rounds $number to $precision digits past the decimal point. The value of +$precision is expected to be a positive integer, any other value will +result in an exception. + + 5.625512332->round_to_precision(3); # 5.626 + 5.5->round_to_precision(10); # 5.5 =head3 is_number diff --git a/t/number.t b/t/number.t index 782ab53..200a8fa 100644 --- a/t/number.t +++ b/t/number.t @@ -26,7 +26,7 @@ is( (-3.49)->round, -3 ); is( 13->round_to_nearest(5), 15); is( 1.23->round_to_nearest(.05), 1.25); -is( 5.6255->round_to_precision(3), 5.626); +is( 5.625512332->round_to_precision(3), 5.626); is( 5.5->round_to_precision(10), 5.5); eval{5.5->round_to_precision(-10.3)}; ok( $@ =~ m/^round_to_precision does not support non-positive non-integer values/); From 98c97f6b536cdab1ed93022e20a5c87eb899777f Mon Sep 17 00:00:00 2001 From: ben hengst Date: Wed, 20 Mar 2013 19:01:42 -0700 Subject: [PATCH 09/11] making the testing changes as requested by @schwern --- t/number.t | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/t/number.t b/t/number.t index 200a8fa..c7ca5cc 100644 --- a/t/number.t +++ b/t/number.t @@ -28,8 +28,8 @@ is( 1.23->round_to_nearest(.05), 1.25); is( 5.625512332->round_to_precision(3), 5.626); is( 5.5->round_to_precision(10), 5.5); -eval{5.5->round_to_precision(-10.3)}; -ok( $@ =~ m/^round_to_precision does not support non-positive non-integer values/); +ok eval{5.5->round_to_precision(-10.3)}; +like( $@, m/^round_to_precision does not support non-positive non-integer values/); ok( 12->is_number ); ok(!'FF'->is_number ); From f49dc4c662ad6ca77fee1e63c0616b07723c7531 Mon Sep 17 00:00:00 2001 From: ben hengst Date: Thu, 11 Jul 2013 19:39:58 -0700 Subject: [PATCH 10/11] fixing the issue that @schwern pointed out in https://github.com/schwern/perl5i/pull/240#commitcomment-2863941 --- t/number.t | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/number.t b/t/number.t index c7ca5cc..e8f59ad 100644 --- a/t/number.t +++ b/t/number.t @@ -29,7 +29,7 @@ is( 1.23->round_to_nearest(.05), 1.25); is( 5.625512332->round_to_precision(3), 5.626); is( 5.5->round_to_precision(10), 5.5); ok eval{5.5->round_to_precision(-10.3)}; -like( $@, m/^round_to_precision does not support non-positive non-integer values/); +like( $@, qr/^round_to_precision does not support non-positive non-integer values/); ok( 12->is_number ); ok(!'FF'->is_number ); From 0c1e0701ef610be0cdfb45e86cfc44a1f8c06d6a Mon Sep 17 00:00:00 2001 From: ben hengst Date: Thu, 11 Jul 2013 19:50:55 -0700 Subject: [PATCH 11/11] blarg --- t/number.t | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/number.t b/t/number.t index e8f59ad..bc72ebb 100644 --- a/t/number.t +++ b/t/number.t @@ -28,7 +28,7 @@ is( 1.23->round_to_nearest(.05), 1.25); is( 5.625512332->round_to_precision(3), 5.626); is( 5.5->round_to_precision(10), 5.5); -ok eval{5.5->round_to_precision(-10.3)}; +eval{5.5->round_to_precision(-10.3)}; like( $@, qr/^round_to_precision does not support non-positive non-integer values/); ok( 12->is_number );