in reply to best way to write code

There are lots of ways to do this, (some listed above by others).
One thing I can see wrong with your code is you are calling
localtime
more than once. This can lead to inconsistancies in the numbers. For formatting, try this:
my @time = localtime; my $yyyy = $time[5] + 1900; my $mm = sprintf("%02d", $time[4] + 1); # pad with 0's # repeat for other variables