Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
每次脚本运行时,将Perl哈希转储到新的txt文件中_Perl_Output_Perl Data Structures_Data Dumper - Fatal编程技术网

每次脚本运行时,将Perl哈希转储到新的txt文件中

每次脚本运行时,将Perl哈希转储到新的txt文件中,perl,output,perl-data-structures,data-dumper,Perl,Output,Perl Data Structures,Data Dumper,我有一个Perl脚本,它将散列转储到“output.txt”文件中。问题是,每次我运行这个脚本时,相同的“output.txt”文件都会被覆盖。如何在每次运行脚本时生成一个新的“.txt”文件,以便每次运行的结果都包含在单独的文件中 我现在在perl脚本的末尾有这样的内容: print Dumper( \%data ); open my $temp, '>', 'output.txt' or die $!; print $temp Dumper \%data; close $temp;

我有一个Perl脚本,它将散列转储到“output.txt”文件中。问题是,每次我运行这个脚本时,相同的“output.txt”文件都会被覆盖。如何在每次运行脚本时生成一个新的“.txt”文件,以便每次运行的结果都包含在单独的文件中

我现在在perl脚本的末尾有这样的内容:

print Dumper( \%data );
open my $temp, '>', 'output.txt' or die $!;
print $temp Dumper \%data;
close $temp;

您可以使用时间戳作为文件名的一部分。我猜你的脚本每秒运行的次数不会超过一次

因此,使用

my $time = time; # seconds since 1970-01-01
my $filename = "output_$time.txt"; # output_1427737784.txt

如果您需要更多的文件名,或者只是不喜欢时间戳,那么就可以解决这个问题。它不仅为您创建一个随机文件名,而且还立即打开文件(防止死锁),并返回一个文件句柄

use File::Temp 'tempfile';
my ($fh, $filename) = tempfile(); # replaces open()

因此,当我尝试第一种方法时,我得到了这样一条消息:在使用“strict refs”时,不能使用string(“output_1427778835.txt”)作为符号ref。这是什么意思?哦,糟糕,对不起。我使用了带有时间戳的输出文件作为文件的实际名称。这就是我收到错误消息的原因。你的建议现在很管用!谢谢!:)@Daniel..同一问题的另一个扩展:有没有一种方法可以给这些输出文件唯一的名称,而不是在名称中加上时间戳?
use File::Temp 'tempfile';
my ($fh, $filename) = tempfile(); # replaces open()