Linux perl mechanize遇到了一些限制-已开始多次调试尝试

Linux perl mechanize遇到了一些限制-已开始多次调试尝试,linux,perl,debugging,printf,mechanize,Linux,Perl,Debugging,Printf,Mechanize,您好,亲爱的开发人员 首先,很抱歉我是新手。。我对Perl非常陌生 我试图学习一些关于perl的知识,同时使用代码和代码片段。 今天我有一个运行机械化作业的小脚本。。但有些事情并没有结束。目的是:我想得到一些wesite sceenshots的缩略图 我运行了这个脚本,它是用来做一些我已经启动并运行mozrepl的网站截图的。奇怪的是输出-见下文。。。问题:我应该更改脚本吗?为什么要更改输出 #!/usr/bin/perl use strict; use warnings; use WWW::M

您好,亲爱的开发人员

首先,很抱歉我是新手。。我对Perl非常陌生

我试图学习一些关于perl的知识,同时使用代码和代码片段。 今天我有一个运行机械化作业的小脚本。。但有些事情并没有结束。目的是:我想得到一些wesite sceenshots的缩略图

我运行了这个脚本,它是用来做一些我已经启动并运行mozrepl的网站截图的。奇怪的是输出-见下文。。。问题:我应该更改脚本吗?为什么要更改输出

#!/usr/bin/perl
use strict;
use warnings;
use WWW::Mechanize::Firefox;

my $mech = new WWW::Mechanize::Firefox();

open(INPUT, "<urls.txt") or die $!;

while (<INPUT>) {
        chomp;
        print "$_\n";
        $mech->get($_);
        my $png = $mech->content_as_png();
        my $name = "$_";
        $name =~s/^www\.//;
        $name .= ".png";
        open(OUTPUT, ">$name");
        print OUTPUT $png;
        sleep (5);
}
#/usr/bin/perl
严格使用;
使用警告;
使用WWW::Mechanize::Firefox;
my$mech=new WWW::Mechanize::Firefox();
打开(输入“$name”);
打印输出$png;
睡眠(5);
}
代码给badck的是以下内容

http://www.unifr.ch/sfm
print() on closed filehandle OUTPUT at test_3.pl line 20, <INPUT> line 2.
http://www.zug.phz.ch
print() on closed filehandle OUTPUT at test_3.pl line 20, <INPUT> line 3.
http://www.schwyz.phz.ch
print() on closed filehandle OUTPUT at test_3.pl line 20, <INPUT> line 4.
http://www.luzern.phz.ch
print() on closed filehandle OUTPUT at test_3.pl line 20, <INPUT> line 5.
http://www.schwyz.phz.ch
print() on closed filehandle OUTPUT at test_3.pl line 20, <INPUT> line 6.
http://www.phvs.ch
print() on closed filehandle OUTPUT at test_3.pl line 20, <INPUT> line 7.
http://www.phtg.ch
print() on closed filehandle OUTPUT at test_3.pl line 20, <INPUT> line 8.
http://www.phsg.ch
print() on closed filehandle OUTPUT at test_3.pl line 20, <INPUT> line 9.
http://www.phsh.ch
print() on closed filehandle OUTPUT at test_3.pl line 20, <INPUT> line 10.
http://www.phr.ch
http://www.unifr.ch/sfm
test_3.pl第20行第2行关闭的文件句柄输出上的print()。
http://www.zug.phz.ch
test_3.pl第20行第3行关闭的文件句柄输出上的print()。
http://www.schwyz.phz.ch
在test_3.pl第20行第4行的关闭文件句柄输出上打印()。
http://www.luzern.phz.ch
test_3.pl第20行第5行关闭的文件句柄输出上的print()。
http://www.schwyz.phz.ch
在test_3.pl第20行第6行的关闭文件句柄输出上打印()。
http://www.phvs.ch
test_3.pl第20行第7行关闭的文件句柄输出上的print()。
http://www.phtg.ch
在test_3.pl第20行第8行的关闭文件句柄输出上打印()。
http://www.phsg.ch
在test_3.pl第20行第9行关闭的filehandle输出上打印()。
http://www.phsh.ch
test_3.pl第20行,第10行关闭的文件句柄输出上的print()。
http://www.phr.ch
我的所做的一切都是为了摆脱这些问题: 嗯,我可以使用诊断术语来了解发生了什么。。。 或者,关闭的filehandle输出上的print()也为我们提供了许多 将告诉我们,我们没有使用autodie,也没有检查open的返回值

嗯,我只是在思考文件句柄

嗯:open调用失败了,因为您假设它成功并继续尝试使用filehandle(未打开),所以您收到了该错误

这里要学习的教训是,我们应该始终检查打开调用的返回代码,以验证它是否成功,如果没有成功,则采取适当的操作

嗯-我想我必须在这里学习一些perl问题。。。我想我必须相应地修改代码

我们还应该注意,应该使用open的3 arg形式和filehandle的词法变量

嗯,这个怎么样。 代码:

打开我的$out\u fh,'>',$name或die“无法创建/打开'$name'”;
我只需要将这部分构建到原始代码中。。你觉得怎么样

#!/usr/bin/perl




use strict;
use warnings;
use WWW::Mechanize::Firefox;

my $mech = new WWW::Mechanize::Firefox();

open my $out_fh, '>', $name or die "failed to create/open '$name' <$!>";


open(INPUT, "<urls.txt") or die $!;

while (<INPUT>) {
        chomp;
        print "$_\n";
        $mech->get($_);
        my $png = $mech->content_as_png();
        my $name = "$_";
        $name =~s/^www\.//;
        $name .= ".png";
        open(OUTPUT, ">$name");
        print OUTPUT $png;
        sleep (5);
}
#/usr/bin/perl
严格使用;
使用警告;
使用WWW::Mechanize::Firefox;
my$mech=new WWW::Mechanize::Firefox();
打开我的$out_fh,'>',$name或die“未能创建/打开'$name'”;
打开(输入“$name”);
打印输出$png;
睡眠(5);
}
嗯 你觉得怎么样


您将如何更改代码-并确保脚本将成功运行

首先,您的输入包含斜杠,然后您试图使用该输入创建文件名。因为您的输入以“
http://www
”而不是“
www
”,您的替换操作也不会执行任何操作

my $name = "$_";            # e.g. $name <= "http://www.zug.phz.ch"
$name =~s/^www\.//;         # $name still is "http://www.zug.phz.ch"
$name .= ".png";            # $name is ""http://www.zug.phz.ch.png"
open(OUTPUT, ">$name");     # error: no directory named "./http:"
print OUTPUT $png;
sleep (5);
如果有什么需要检查的,则返回
while
循环中的
open
调用中的值。如果你说

open(OUTPUT,">$name") or warn "Failed to open '$name': $!";

您可能会自己解决这个问题。

您好,亲爱的暴徒-非常感谢您的快速回复。我按建议去做。然后回来报告所有的发现。。。。
$name =~ s![:/]+!-!g; #http://foo.com/bar.html  becomes  http-foo.com-bar.html
open(OUTPUT,">$name") or warn "Failed to open '$name': $!";