Perl FTP“GLOB”导致空白条目?

发布于 2025-01-12 17:54:36 字数 1732 浏览 3 评论 0原文

我有一个 perl 脚本正在尝试 FTP 文件,但似乎所有其他文件都失败了。 我确定由于某种原因

$filename = glob $filename 

$filename 设置为空而不是完整的文件名。

我在 glob 行之前打印出了文件名,它是正确的并且其中没有空格。

我也尝试只注释掉 glob 行,但随后它返回“不是 GLOB 引用”,所以显然 net ftp 需要这样做。

知道什么可能导致 glob 返回空吗? (以及为什么它适用于第一个文件和之后的每个第二个文件)

这就是我发送给子文件的内容:

ftpToDevice($ftp,$device_dir,$config,$config,'put');

这是实际的子文件:

sub ftpToDevice {
        my ($ftp,$fileDir,$fileName,$fileNameDest,$action) = @_;
        print "ftp filename $fileName\n";
        $fileName =~ s/\s+//g;
        $fileNameDest =~ s/\s+//g;
        $fileName = glob($fileName);
        my $path = "cf3:\\";
        chdir $fileDir if $action eq 'put';                 

        print "file location: $fileDir/$fileName to $fileNameDest\n";

        if($ftp->cwd("$path")){
                if($action eq 'put'){
                        print "attempting FTP PUT $fileName $fileNameDest\n";
                        $ftp->put($fileName,$fileNameDest) or return "Error cannot put - " . $ftp->message;
                        #$ftp->rename($fileName,$fileNameDest) or return "Error cannot put - " . $ftp->message;
                } else {
                        print "Attempting to delete $path $fileNameDest\n";       
                        #$ftp->delete("$fileName") or return "Error cannot delete $fileName - " . $ftp->message;
                        $ftp->delete($fileNameDest) or return "Error cannot delete $fileName - " . $ftp->message;
                        print $ftp->message . "\n";
                }
        }
        chdir $masterDir if $action eq 'put';
        return 'success';
}

I have a perl script that is attempting to FTP files and it seems to be failing every other file.
I determined that for some reason the

$filename = glob $filename 

is setting $filename to empty rather than the full filename.

I printed out the filename just before the glob line and it is correct and has no spaces in it.

I also tried just commenting out the glob line but then it returns "Not a GLOB reference" so apparently net ftp requires that.

Any idea what might cause glob to return empty? (and why it works on the first file and every 2nd file after that)

This is what I'm sending to the sub:

ftpToDevice($ftp,$device_dir,$config,$config,'put');

Here is the actual sub:

sub ftpToDevice {
        my ($ftp,$fileDir,$fileName,$fileNameDest,$action) = @_;
        print "ftp filename $fileName\n";
        $fileName =~ s/\s+//g;
        $fileNameDest =~ s/\s+//g;
        $fileName = glob($fileName);
        my $path = "cf3:\\";
        chdir $fileDir if $action eq 'put';                 

        print "file location: $fileDir/$fileName to $fileNameDest\n";

        if($ftp->cwd("$path")){
                if($action eq 'put'){
                        print "attempting FTP PUT $fileName $fileNameDest\n";
                        $ftp->put($fileName,$fileNameDest) or return "Error cannot put - " . $ftp->message;
                        #$ftp->rename($fileName,$fileNameDest) or return "Error cannot put - " . $ftp->message;
                } else {
                        print "Attempting to delete $path $fileNameDest\n";       
                        #$ftp->delete("$fileName") or return "Error cannot delete $fileName - " . $ftp->message;
                        $ftp->delete($fileNameDest) or return "Error cannot delete $fileName - " . $ftp->message;
                        print $ftp->message . "\n";
                }
        }
        chdir $masterDir if $action eq 'put';
        return 'success';
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨小沫ゞ 2025-01-19 17:54:36

我找到了一个解决方案,但不确定为什么它有效。
我注释掉了这一行:

$fileName = glob($fileName);

我改为添加了这一行:

$fileName = "" . $fileName;  #this converts it to a string I believe

这允许所有文件都可以工作(不仅仅是每隔一个文件)并且不会给出 GLOB 错误。

我的理论是,不知何故,在使用 ftp sub 的 for every 循环之前生成的文件列表导致每个第二个文件成为 glob 对象(数组或其他东西?),而不是纯文本文件名。

I found a solution but am not sure why it worked.
I commented out the line:

$fileName = glob($fileName);

I added this instead:

$fileName = "" . $fileName;  #this converts it to a string I believe

This allowed all files to work (not just every second one) and did not give the GLOB error.

My theory is that somehow the list of files that is generated before the for each loop that uses the ftp sub is causing every second file to be a glob object (array or something?) rather than a plain text filename.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文