Re^3: perl basic count days between two datesby jethro (Monsignor)
|on Oct 02, 2013 at 21:59 UTC||Need Help??|
Ok, this code:
What happens when NumberOfDaysBetween() is not 365? $fullYear is undefined. Since you have warnings enabled you should see a corresponding warning message. It will still evaluate to 0, but initializing the variable "my $fullYear=0;" is better.
Also, why call NumberOfDaysBetween() with $endDay twice as parameter? Shouldn't there be a $sMon instead?
But if you want to program yourself you surely would prefer finding the bugs yourself instead of us just telling you what to do. So here is how:
It is very fortunate that you are already using subroutines. You first want to make sure that your subroutines are correct. Before you can check what is wrong with the subroutine FullYear you have to be sure that NumberOfDaysBetween really does what you expect it to do. So lets do that:
After the line "use warnings;" at the beginning of your program add the following lines:
Now call your script. Is the output as you expect it? If yes, lets be sure and put in some other values and check these too. If you are finally convinced that NumberOfDaysBetween is correct, then do the same with isFullYear(). And so on, until there is nothing left to test
But what happens when you find unexpected results? Then you should add print-statements in that subroutine where you print out the values of variables. Or simply check which path the program takes in an if-clause. For example:
directly after that variable was set would tell you if the program executed the line before and what the result was. Start the program and check if the output you are getting is as you expected. Put in as many print statements as you need, the only downside is that you have to delete them again after you found the bugs
That is everything you need to know at the moment. Should you later have more complex data structures like arrays and hashes I recommend you take a look at the module Data::Dumper to print out those. Also you should know that there is a debugger built into perl. Instead of putting in all those print statements you can step through the program line by line and print out variables while the programm is running without having to change one line of code. But for the moment print-statements will do.