[ABE.pm] making things more perlish

Faber Fedor faber at linuxnj.com
Fri Jan 7 11:17:43 PST 2005


Heyguys,

I just found a nasty little bug in one of my date routines (turns out
when date(1) substracts a month, it subtracts 30 days instead) so I
rewrote it using DateTime.

What I have know does what I want (and more!) so it's going into the
codebase, but it's not very Perl-ish; I especially didn't like having to
create the second DateTime object.

I was wondering if anyone in here would like to show me how to make it
more Perl-ish?


<----------------------------< cut here >---------------------------->
#!/usr/bin/perl -w

use strict;
use DateTime;

my $mydate = ldopm();
print "$mydate\n";
$mydate = ldopm("20041231");
print "$mydate\n";
$mydate = ldopm(ldopm());
print "$mydate\n";


sub ldopm {
# Get the last day of the previous month

    my ($date) = @_;
    my $dt;

    if ($date) { $dt= DateTime->new(year  => substr($date, 0, 4),
                                    month => substr($date, 4, 2),
                                    day   => substr($date, 6, 2)
            ); }
    else {

        $dt = DateTime->now;
    }


    $dt = $dt->subtract(months => 1);

    my ($year, $month);

    $year = $dt->year();
    $month = $dt->month();

    my $dt2 = DateTime->last_day_of_month( year=> $year, month=> $month
);

    return $dt2->ymd('');

}



-- 
 
Regards,
 
Faber                     

Linux New Jersey: Open Source Solutions for New Jersey
http://www.linuxnj.com





More information about the ABE-pm mailing list