M. Lewis wrote:
>
> Given the following code, if I were to want $day, $month, $hour, $minute
> & $sec to have a leading zero (ie 01 for Jan rather than 1), is my only
> option to use printf? Or is there a better way.
>
> What I'm searching for here is the *correct* method to get $day, $month,
> etc for uses like naming backup files (databackup-2007-01-21.tar.gz).
>
> Thanks,
> Mike
>
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> my($sec, $min, $hour, $day, $month, $year)=(localtime)[0 .. 5];
>
> print "day=$day\n";
> print "month=".($month+1)."\n";
> print "year=".($year+1900)."\n\n";
> print "hour=$hour\n";
> print "minute=$min\n";
> print "second=$sec\n\n";

Hi Mike

I'm not sure exactly what you're looking for, as your code doesn't address the
question of naming backup files.

I would write a short subroutine to 'fix' the output from localtime, and use
sprintf to build the filename. I hope this is what's wanted.

Rob


use strict;
use warnings;

sub date {
  my @date = localtime;
  $date[5] += 1900;
  $date[4]++;
  @date;
}

my $backup = sprintf 'databackup-%d-%02d-%02d.tar.gz', (date)[5,4,3];

print $backup, "\n";

**OUTPUT**

databackup-2007-01-21.tar.gz

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to