一两件事我注意到...
for $file ($ftp -> ls())
{
$bar = file;
这应该是$bar = $file
而不是$bar = file
(脚本缺少龙头$
在file
)。否则,您只需将字符串file
置入$bar
即可。
你表达的另一部分是:
$dst_dir =~ s/\$\&/\$bar/g; #so this is replacing \$\& with $src
$dst_pattern =~ s/\$\&/\$bar/g;
什么是$dst_dir
和$dst_pattern
的价值?这是真实问题在这里。
Somewhere $dst_dir
and $dst_pattern
is being set。这是您将FTP中的文件替换为这些字符串的地方。然后我注意到这一点:
$dst_dir1 = eval($dst_dir);
$dst_file = eval($dst_pattern);
看来,$dst_dir
和$dst_file
是某种形式的命令?为什么还要用eval
在其上运行?这两个字符串的值是什么?为什么通过eval
运行?
发生了什么是这两个命令的字符串为$&
,并且您正在替换该文件。我们假设$dst_dir
等于$ftp->get("$&")
。您从$ftp->ls
命令,在文件名(假设它的bar.txt
代入这个字符串因此,$dst_dir1
设置为$ftp->get("bar.txt");
而综观目前整个循环:。
for $file ($ftp -> ls())
{
$bar = file;
$dst_dir =~ s/\$\&/\$bar/g; #so this is replacing \$\& with $src
$dst_pattern =~ s/\$\&/\$bar/g;
$dst_dir1 = eval($dst_dir);
$dst_file = eval($dst_pattern);
}
我看到另一个问题。你正在循环每个文件,并且每次替换$dst_dir
和$dst_pattern
中的$&
。但是,如果你正在为每个文件执行此操作,并且没有重置$dst_dir
和$dst_pattern
的原始值,那意味着你第二次经过你的循环,你不会改变$dst_dir
和$dst_pattern
。而且,所有其他时间你也要经历你的循环。
您还没有检查以确保替换实际上有效,并且您没有检查eval
是否通过检查[email protected]
的值来检查。
最重要的是,你没有设置use strict;
,可能不是use warnings;
。
这里是循环的新版本:
for my $file ($ftp->ls) {
my $dist_dir = $dst_dir; # Make sure you don't futz with your
my $dist_pattern = $dst_pattern; # original templates!
# Check to make sure replacements work!
if (not $dist_dir =~ s/\$\&/\$file/g) {
die qq(Couldn't put file name "$file" into "$dist_dir");
}
if (not $dist_pattern =~ s/\$\&/\$file/g) {
die qq(Couldn't put file name "$file" into "$dist_pattern");
}
# Check for Eval!
my $dst_dir1;
my $dst_file1;
$dst_dir1 = eval($dist_dir);
if ("[email protected]") {
die qq(Evaluation of `$dist_dir` failed!: [email protected]);
}
$dst_file1 = eval($dist_pattern);
if ("[email protected]") {
die qq(Evaluation of '$dist_pattern' failed!: [email protected]);
}
}
这是检查,以确保置换工作,并且还节约每次不修改您的模板。
但是$&之前没有$ _,所以它没有引用上述 –
'$&'只是'$ foo'中的文本文本,它正在被替换。 – Barmar
@LukeMakk - 由于正则表达式中的反斜杠,'$&'不被视为Perl特殊变量,而仅仅被视为字符序列。 '$ _'与这里发生的事情无关(除了可能在$ foo中)。 –