I'm not able to change the dir in perl.

#!/usr/bin/perl
use strict;

my $dir=`date +%d%b%Y`;
#the output of $dir is 13Feb2014 that directory is already inside "/mnt/Recordings/Inbound/Kinrep/" 

my $path = "/mnt/Recordings/Inbound/Kinrep/$dir";        
chdir($path) or die "Cant chdir to $path $!";

whenever i'm executing my program i'm not able to change the directory i got following error.

Cant chdir to /mnt/Recordings/Inbound/Kinrep/13Feb2014
 No such file or directory at Ftp_transfer_197.pl line 17.
有帮助吗?

解决方案

chomp $dir; will remove the newline that the command in the backticks appended to its output. It's very rare that a directory name actually contains a newline, although it's possible on many file systems.

其他提示

You have a solution, which you have accepted. But I wanted to point out that this is a side-effect of you using an external program to do something that you can do perfectly well within Perl. There are many ways to do what you want. The simplest is probably to use the (standard) Time::Piece module.

use Time::Piece;

$dir = localtime->strftime('%d%b%Y');
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top