Think about Loose Coupling | |
PerlMonks |
Re: perl basic count days between two datesby davido (Cardinal) |
on Oct 02, 2013 at 22:08 UTC ( [id://1056684]=note: print w/replies, xml ) | Need Help?? |
We were all new to Perl at some point, and all new to programming in general. Nothing wrong with that. But this is not a "basic" problem. However, Perl does provide tools to help you avoid becoming an expert in date calculations. My advice: If you're not using the power of Perl's modules (core and CPAN), you're not programming Perl. You're just using its syntax and interpreter. Your technique is neglecting leap years, isn't it? Here is a CPANy version:
A sample run:
(I used the birth date of a famous individual in the Perl community.) Date calculations are very hard to get right. At minimum we have to deal with leap years. There could be other challenges that I'm not aware of, and since I don't want to become an expert in nuances of the Gregorian Calendar, I offload as much of that burden as possible to the DateTime module. Prompting with a message isn't all that hard, but IO::Prompt::Tiny seems more convenient than a bunch of print statements, <STDIN> invocations, and chomp, so I used that module too. Once we've handled the prompting and the date manipulation, there's not much left to do except a quick sanity check, and printing the result of the calculation. Keep in mind that there are countless problem domains in the universe. As a programmer you will be asked to become an expert on some of them. But it would be impossible to become an expert on all of them. Using well-tested modules that solve problems such that you don't need to become an expert on a peripheral topic will enable you to focus more on becoming a great programmer, and less on becoming a great date calculator, genome researcher, and so on. There's no shame in using available tools to make your life as a programmer easier. In fact, it's smart; you become more effective, and your code becomes more robust. A little more terse:
Dave
In Section
Seekers of Perl Wisdom
|
|