Unable to create logfile with local time stamp using perl

Hello All,

Morning,

I am facing problem with my code while creating a log with name as current time stamp using perl. Here is the code.

#!/usr/bin/perl
my $time=localtime;
my ($day,$month,$date,$tm,$year)=split(/ /,$time);
my $stamp=$year."_".$month."_".$date;
my $logdir="SAP_IN_".$stamp;
my $LOGFILE = "E:/pearl/$logdir.log" ;

print " Log file name is : $LOGFILE ";

out put:

E:/pearl/SAP_IN_12:09:04_Feb_.log

It should be

E:/pearl/SAP_IN_12:09:04_Feb_3.log.

I think value of localtime gives date in single digit hence split is failing to get value. But this code works if date in 2 digit.

Can some one help me to correct the code?

Thanks
Krsna..

#!/usr/bin/perl
my $time=localtime;
my ($day,$month,$date,$tm,$year)=split(/\s+/,$time);
my $stamp=$tm."_".$month."_".$date;
my $logdir="SAP_IN_".$stamp;
my $LOGFILE = "E:/pearl/$logdir.log" ;

print "Log file name is : $LOGFILE\n";

Thanks a lot Bala,

This is perfect one working fine. Just want to know what is exact meaning of split(/\s+/,$time); what is the significance of pattern "/s+/".
Please explain.

Thanks
Krsna..

\s matches all whitespaces (tabs, newline, space...)
+ is a quantifier which tells perl to search for one or more of \s.

1 Like

Thanks a lot Bala,

Simply excelent...