in reply to Re^2: Get DoB Bounds from Age Range
in thread Get DoB Bounds from Age Range

Suggestions are suggestions: you cherry pick them according to your needs and tastes. :) However ... where is your science? Simply "feeling" that these default values will not work for is not the same as actually testing them out.

use strict; use warnings; use Test::More qw( no_plan ); use DateTime; for my $min (1 .. 20) { for my $max (30 .. 80) { for my $inc ( 0 .. 1 ) { is_deeply [ jeffa( $min, $max, $inc ) ], [ over2sd( $min, $max, $inc ) ], ; } } } done_testing(); sub jeffa { my $min = shift || 0; my $max = shift || 0; my $inclusive = $_[0] ? 1 : 0; my ($xs,$ns) = (DateTime->now,DateTime->now); $xs->subtract(years => $min, days => -$inclusive); $ns->subtract(years => $max, days => 364 + $inclusive); return $xs->ymd('-'),$ns->ymd('-'); } sub over2sd { my ($agemin,$agemax,$inclusive) = @_; die "Minimum age omitted in DoBrangefromAges" unless (defined $age +min and $agemin ne ''); $agemin = int($agemin); $agemax = int($agemin) unless defined $agemax; $agemax = int($agemax); $inclusive = ($inclusive ? $inclusive : 0); my ($maxdob,$mindob) = (DateTime->now,DateTime->now); $maxdob->subtract(years => $agemin, days => -$inclusive); $mindob->subtract(years => $agemax, days => 364 + $inclusive); return $maxdob->ymd('-'),$mindob->ymd('-'); }

When i run this test i see no difference in the output of your code and mine. It is certainly possible that i have not tested as fully as can be tested, but i have more certainty that i what i provided will give you the desired output, as was my goal.

UPDATE:
You only need change one line in my sub. From this:

my $max = shift || 0;
to this:
my $max = shift || $min;
Returning 0 if the user passes 0 arguments seems like justice to me. >:)

use Test::More qw( no_plan ); is_deeply [ jeffa( ) ], [ over2sd( ) ], ; is_deeply [ jeffa( 10 ) ], [ over2sd( 10 ) ], ; __END__ Use of uninitialized value $agemin in int at foo.pl line 37. ok 1 ok 2 1..2

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Replies are listed 'Best First'.
Re^4: Get DoB Bounds from Age Range
by over2sd (Beadle) on May 07, 2015 at 21:40 UTC
    The 'science' behind my statement is that treating undefined as 0 for age will result in dates for 0 years, which probably isn't what the user wants.