Keep It Simple, Stupid PerlMonks

### Module Code Review

by TStanley (Canon)
 on Sep 15, 2001 at 03:58 UTC Need Help??

TStanley has asked for the wisdom of the Perl Monks concerning the following question:

Lately I have been using a particular set of code over and over again, and I finally decided to put it into a module. I would like everyone's overall opinion of this code, and whether or not they think it might even be worthy of CPAN.

TStanley
--------
There's an infinite number of monkeys outside who want to talk to us
-- Douglas Adams/Hitchhiker's Guide to the Galaxy

Replies are listed 'Best First'.
Re: Module Code Review
by japhy (Canon) on Sep 15, 2001 at 04:24 UTC
Your leap year calculation is incorrect. 2004 is a leap year, 2000 is a leap year, 1904 is a leap year, but 1900 is NOT. The rule is:
• divisible by 4, and
• not divisible by 100, or
• divisible by 400
if (!(\$y % 4) and (\$y % 100 or !(\$y % 400))) { ... }
Other than that, here's a much simpler method for finding out yesterday:
use Time::Local; my \$noon = timelocal(0,0,12, (localtime)[3,4,5]); my (\$d,\$m,\$y) = (localtime(\$noon - 86400))[3,4,5];

_____________________________________________________
Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

You don't even need Time::Local,

my \$yesterday= do { my(\$y,\$m,\$d)= (localtime( time-60*60*(12+(localtime)[2]) ))[5,4,3]; sprintf "%04d-%02d-%02d", 1900+\$y, 1+\$m, \$d; };
or, just for fun:
my \$yesterday= join"-",mapcar{sprintf "%0".shift()."d",pop()+pop()} [4,2,2],[(localtime( time-60*60*(12+(localtime)[2]) ))[5,4,3]],[1900,1,0];

- tye (but my friends call me "Tye")
Re: Module Code Review
by merlyn (Sage) on Sep 15, 2001 at 05:34 UTC
I'm not sure why you just didn't use Date::Calc, particularly the Days_in_Month and Calendar functions.

Using an object here seems a bit of an overkill. I'm not sure I'd ever subclass this, and there's no internal state that might change over time.

Re: Module Code Review
by princepawn (Parson) on Sep 15, 2001 at 05:21 UTC
The datetime@perl.org mailing list is reworking the entire Date/Time infrastructure now.

Post your code there for a thorough critique.

Re: Module Code Review
by TStanley (Canon) on Sep 16, 2001 at 05:09 UTC
Thank you all for your honest critiques. The reason I did a lot of this was primarily a learning experience for myself, especially in the area of object oriented programming. Learning how to put a module together and using it, is basically what I was trying to accomplish. And if the material didn't exist on CPAN (fat chance obviously :-) ), I would gladly add it to the repository.

TStanley
--------
There's an infinite number of monkeys outside who want to talk to us
-- Douglas Adams/Hitchhiker's Guide to the Galaxy

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://112582]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2021-07-28 07:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?

No recent polls found

Notices?