Date in Perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Date in Perl
# 1  
Old 11-08-2008
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'`; ?

Code:
#!/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);

}


Last edited by Raynon; 11-08-2008 at 09:11 PM..
# 2  
Old 11-08-2008
I do not have time to simulate running it, but what about replacing with ${date_today} in the grep?
# 3  
Old 11-08-2008
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.
# 4  
Old 11-09-2008
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?

Code:
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Today's date using Perl?

Easiest way to get it in the form of MM/DD/YY for Perl 5.8.8? Thanks (4 Replies)
Discussion started by: stevensw
4 Replies

2. Shell Programming and Scripting

Fetch date of 7 years back from current date in Perl

$beginDate = substr(DateCalc("today", "-7Days"),0,8); This fetches the date 7 days back Can I fetch the date before 7 years from todays date in Perl using same syntax Use code tags, see PM. (3 Replies)
Discussion started by: parthmittal2007
3 Replies

3. Shell Programming and Scripting

Extract week start,end date from given date in PERL

Hi All, what i want to do in perl is i should give the date at run time .Suppose date given is 23/12/2011(mm/dd/yyyy) the perl script shold find week start date, week end date, previous week start date,end date,next week start date, end date. In this case week start date will be-:12/19/2011... (2 Replies)
Discussion started by: parthmittal2007
2 Replies

4. Shell Programming and Scripting

Need to capture dates between start date and end date Using perl.

Hi All, Want to get all dates and Julian week number for that date between the start date and end date. How can I achive this using perl? (To achive above functionality, I was connecting to the database from DB server. Need to execute the same script in application server, since databse... (6 Replies)
Discussion started by: Nagaraja Akkiva
6 Replies

5. Shell Programming and Scripting

Passing date formats in Perl: i.e. Jul/10/2007 -> 20070710 (yyyymmdd) - Perl

Hi , This script working for fine if pass script-name.sh Jul/10/2007 ,I want to pass 20070710(yyyymmdd) .Please any help it should be appereciated. use Time::Local; my $d = $ARGV; my $t = $ARGV; my $m = ""; @d = split /\//, $d; @t = split /:/, $t; if ( $d eq "Jan" ) { $m = 0 }... (7 Replies)
Discussion started by: akil
7 Replies

6. Shell Programming and Scripting

date in perl

im having trouble with this... i can do it in awk but perl is giving me trouble. i want to test for the previous 3 days, today, and the coming three days. heres the code: #!/usr/bin/perl chomp($info=`date +%b%d`); $info =~ tr///; if ($info =~ m/^(*)()$/) { $info = "$1.0.$2"; } ... (4 Replies)
Discussion started by: nomkev
4 Replies

7. Shell Programming and Scripting

Perl: Extracting date from file name and comparing with current date

I need to extract the date part from the file name (20080221 in this ex) and compare it with the current date and delete it, if it is a past date. $file = exp_ABCD4_T-2584780_upto_20080221.dmp.Z really appreciate any help. thanks mkneni (4 Replies)
Discussion started by: MKNENI
4 Replies
Login or Register to Ask a Question