Perl 如何将响应输出到文本文件并在完成后打开文本文件
我想修改一个Perl脚本,将结果保存到文本文件output.txt中,并在任务完成时打开results output.txt 我试过严格使用; 使用警告;和其他方法,但我不断得到一个错误'最后一个错误是无法打开output.txtPerl 如何将响应输出到文本文件并在完成后打开文本文件,perl,file-io,Perl,File Io,我想修改一个Perl脚本,将结果保存到文本文件output.txt中,并在任务完成时打开results output.txt 我试过严格使用; 使用警告;和其他方法,但我不断得到一个错误'最后一个错误是无法打开output.txt #!/usr/bin/perl use HTTP::Request; use LWP::UserAgent; system('cls'); system "color c"; print"\n"; print" |||||||||||||||||Robots sc
#!/usr/bin/perl
use HTTP::Request;
use LWP::UserAgent;
system('cls');
system "color c";
print"\n";
print" |||||||||||||||||Robots scanner|||||||||||||||||||||";
print "\n";
print " Scan Your site Site\n\n Example: www.test.com \n\n-> ";
$site=<STDIN>;
chomp $site;
if($site !~ /http:\/\//) { $site = "http://$site/"; };
print "\n";
@path = ('robotx.txt','robots.txt','robot.txt','search',);
foreach $robots(@path){
$url = $site.$robots;
$req = HTTP::Request->new(GET=>$url);
$useragent = LWP::UserAgent->new();
$response = $useragent->request($req);
my $filename = 'report.txt';
open(my $fh, '>', $filename) or die "Could not open file '$filename' $!";
if ($response->is_success){
print ">>>>>>Robots Found !!!>>>: $url\n";
print $fh "out put has been generated by perl\n"; # THIS DOESN'T WORK THE report.txt is empty
print "done\n";
}else{
print "NotFound : $robots\n";
print "done\n";
# I want to open the file from windows explorer automatically here when its done
close $fh;
}
}
以管理员身份运行cmd后,可以看到report.txt文件显示为空
我希望看到人们的反应不那么积极
我还希望perl能够打开report.txt,而不必转到windows资源管理器并由用户手动打开它。我希望它在完成后自动打开,但我无法实现这一点
谢谢 您似乎总是覆盖您的报告文件 您的open在foreach循环中。循环完成4次。您将只收到上次运行的输出,因为前3个文件创建将被最后一个文件覆盖 你应该把开环和关环放在环路之外 如果您只需要第一个结果,您可以通过在If的then部分末尾添加最后一条语句来退出循环。尝试将openmy$fh,'>',$filename更改为openmy$fh,'>>,$filename两个箭头而不是一个箭头。这将添加每个新条目,而不是删除最后一个条目 以下是有关使用Perl打开Windows文件的一些答案:
在循环中打开和关闭同一个文件比在循环外打开和关闭有什么好处?我已经修复了代码的缩进。当然欢迎你,但请考虑以后自己做。良好的缩进是帮助人们理解代码的强大工具。如果您想让人们阅读并理解您的代码,那么最好正确使用缩进。