Perl 打开动态生成的文件

标签 perl file-io backticks

我正在为我的网站使用 CGI 脚本。我在打开动态生成的文件时遇到问题。这是代码:

#!/usr/bin/perl
my @output = `/export/es_share/Zhou/./notification_finder.sh range $date $time $range $ulh TestProd1 $actionname`;
my $filen = $output[0];
open(my $result, "<", $filen) or die "Can't open $filen - $!";
Do something with the file...

总是失败,输出:

Can't open /var/tmp/notification-finder-1375086676-658183725.tmp - 
No such file or directory at /var/www/cgi-bin/appsupport/logapp_test/perltest.cgi line X.

虽然成功了:

#!/usr/bin/perl
open(my $result, "<", /var/tmp/notification-finder-1375086676-658183725.tmp) or die "Can't open $filen - $!";
Do something with the file...

我也检查过是否是反引号问题的异步执行问题,但从我对 stackoverflow 的研究来看,这似乎不是问题。我也尝试过这个:

#!/usr/bin/perl
my @output = `/export/es_share/Zhou/./notification_finder.sh range $date $time $range $ulh TestProd1 $actionname`;
sleep(10);
my $filen = $output[0];
open(my $result, "<", $filen) or die "Can't open $filen - $!";
Do something with the file...

我找到了这个similar issue here ,但我似乎没有与提问者相同的问题...感谢您阅读本文。

最佳答案

错误表明$filen末尾有换行符,否则为:

Can't open /var/tmp/notification-finder-1375086676-658183725.tmp - No such file or directory at /var/www/cgi-bin/appsupport/logapp_test/perltest.cgi line XXXXXXXXX.

删除它:

chomp $filen;

关于Perl 打开动态生成的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17919493/

相关文章:

java - 在 Perl 中使用文件参数执行 JAR 文件

mysql - PERL::DBI 在列表中获取用户数据库

mysql - Bugzilla 中分离客户端的基本权限

linux - 将行分隔值转换为语法

mysql - Bash for 循环 : Backticks

perl - 如何在 Perl 读取管道处理程序中扩展变量?

c - 如何用 C 语言从文件中读取文本/数字

java - 检查文件路径是否正确,而不检查文件是否存在

linux - 如果指向的文件被移动或删除,Linux 上打开的文件句柄会发生什么

Mysql在应该的时候没有给出语法错误