Subject: | list context issues |
Hi Shlomi,
You were right about there being possible list context issues with using
<< return if !$self->count >>.
Using some of the methods directly in a hash construction raises the
"Odd number of elements in hash assignment" warning.
Attached is a patch that explicitly returns undef for methods that
should only ever return scalars.
The additional test in descr.t is mostly there to trigger
Test::NoWarnings (which has also been added to Build.PL under
build_requires).
Regards,
Shawn.
Subject: | stats-descr-patch2.diff |
diff -r 075906909733 Statistics-Descriptive/Build.PL
--- a/Statistics-Descriptive/Build.PL Sat Feb 11 12:22:55 2012 +0200
+++ b/Statistics-Descriptive/Build.PL Sun Feb 12 21:28:57 2012 +1100
@@ -17,6 +17,7 @@
'Benchmark' => 0,
'lib' => 0,
'Test::More' => 0,
+ 'Test::NoWarnings' => 0,
},
requires => {
'Carp' => 0,
diff -r 075906909733 Statistics-Descriptive/lib/Statistics/Descriptive.pm
--- a/Statistics-Descriptive/lib/Statistics/Descriptive.pm Sat Feb 11 12:22:55 2012 +0200
+++ b/Statistics-Descriptive/lib/Statistics/Descriptive.pm Sun Feb 12 21:28:57 2012 +1100
@@ -182,7 +182,7 @@
sub standard_deviation {
my $self = shift; ##Myself
- return if (!$self->count());
+ return undef if (!$self->count());
return sqrt($self->variance());
}
@@ -190,7 +190,7 @@
sub variance {
my $self = shift; ##Myself
- return if (!$self->count());
+ return undef if (!$self->count());
my $div = @_ ? 0 : 1;
my $count = $self->count();
@@ -418,7 +418,7 @@
sub median {
my $self = shift;
- return if !$self->count;
+ return undef if !$self->count;
##Cached?
if (! defined($self->_median()))
@@ -438,7 +438,7 @@
}
# check data count after the args are checked - should help debugging
- return if !$self->count;
+ return undef if !$self->count;
$self->sort_data();
@@ -503,7 +503,7 @@
}
# check data count after the args
- return if !$self->count;
+ return undef if !$self->count;
##Cache
my $thistm = join ':',$lower,$upper;
@@ -597,7 +597,7 @@
sub geometric_mean {
my $self = shift;
- return if !$self->count;
+ return undef if !$self->count;
if (!defined($self->_geometric_mean()))
{
diff -r 075906909733 Statistics-Descriptive/t/descr.t
--- a/Statistics-Descriptive/t/descr.t Sat Feb 11 12:22:55 2012 +0200
+++ b/Statistics-Descriptive/t/descr.t Sun Feb 12 21:28:57 2012 +1100
@@ -3,7 +3,8 @@
use strict;
use warnings;
-use Test::More tests => 50;
+use Test::More tests => 52;
+use Test::NoWarnings;
use Benchmark;
use Statistics::Descriptive;
@@ -474,3 +475,19 @@
# TEST
ok ($result == 0, "SD is zero when object has one record.");
}
+
+# Test fucntion returns in list context when no data added.
+# Make sure we have no warnings.
+# The test itsef is almost redundan.
+{
+ my $stat = Statistics::Descriptive::Full->new();
+
+ my %results = (
+ mean => $stat->median,
+ sd => $stat->standard_deviation,
+ );
+
+ # test count needs to allow for Test::NoWarnings, as it adds 1 extra test
+ # TEST*2
+ ok (!defined $results{median}, "get undef under list context when no data added");
+}