Date in Perl

Hi All,

I have a Perl code which performs ftp-ing of files.
However, it seems like the variable $date_today is not passed on to this statement grep { /^ME.*JMR.*$date_today.*sorts$/ } and i can't ftp the files that i wanted.
Can any expert help me to debug this ?
Is there any thing wrong with my date syntax $date_today = `date '+%y%m%d'`; ?

#!/usr/local/bin/perl

use Net::FTP;

$TEMP = "$script_dir/TEMP";

date_format ();

	foreach $t ( 10.12.80.19 ) { 
		chdir "$TEMP";
		print "Now Processing $t ...\n";

    		$ftp = Net::FTP->new("$t", Timeout=>240, Debug => 0)
      		or die "Cannot connect to some.host.name: $@";

    		$ftp->login("login",'password')
      		or die "Cannot login ", $ftp->message;

    		$ftp->cwd("/mydirectory")
      		or die "Cannot change working directory ", $ftp->message;
       		 
		my @lines = grep { /^ME.*KMR.*($date_today|$date_yest).*sorts$/ } $ftp->ls();
			foreach my $filename (@lines) {
    				$ftp->get($filename);
			};

    		$ftp->quit;
	};

sub date_format {

$date_today = `date '+%y%m%d'`;
$date_yest = $date_today - 1;
return ($date_today, $date_yest);

}

I do not have time to simulate running it, but what about replacing with ${date_today} in the grep?

Hi cbkihong,

Tried to put on the curly braces but doesn;t work.

I notice that when i do a print "$date_today, $date_yest\n"; after date_format() , notice that the output is the below.
081109
, 081108

Why would the variable $date_yest which is 081108 be printed out in the next line when i didn;t specify it to print at the next line ?
I strongly believe there's something wrong with the code $date_today = `date '+%y%m%d'`;, but i just do not know what is the correct syntax.

The date command outputs a newline after the date. That's expected because you get the same if you run the same command in a shell.

You can chomp() the variable to remove the trailing newline, but ...

Why don't you just get the date with perl alone in the first place, like this?

use Time::localtime;
$date_today = sprintf("%02d%02d%02d", (localtime->year+1900) % 100, localtime->mon+1, localtime->mday);

If you are willing to install extra module, there are even easier ways to generate arbitrary date strings.