Need some PERL help

jersiq

Senior member
May 18, 2005
887
1
0
Could someone give me some suggestions to correct my PERL script? I'm new at programming in general, and I can't figure out where I am going wrong.

In the beginning of the script I declare the following:

($sec,$min,$hr,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year+=1900;
$mon++;
$bkupset=$year.$mon.$mday;

The thought is that when I run the script, I want to create a directory based on the date that I am running the script in the following format:

/tmp/bkup/yyyymmdd

Now when I run the script today, the directory is labeled as
/tmp/bkup/200773
In other words in single digit months and single digit days, the zeros are truncated I would like them to read 20070703 in the example given above.

 

esun

Platinum Member
Nov 12, 2001
2,214
0
0
Use sprintf. It behaves just like the C function.

http://perldoc.perl.org/functions/sprintf.html

For example,

$mon = sprintf("%02d", $mon);

That will format the month as 01, 02, etc. Do the same for the day.

Actually, you should probably do something like this:

$bkupset = sprintf("%d%02d%02d", $year, $mon, $mday);

Just replace $bkupset=$year.$mon.$mday; with the line above.