为什么这个Perl脚本会永远运行?

时间:2014-03-05 01:55:22

标签: perl loops while-loop do-while

我试图使用perl脚本跟踪亚马逊价格。这应该从亚马逊捕获html并搜索模式并打印前30行。

#!/usr/bin/perl
use strict;
use warnings;
my $pattern = "SuperSaverShipping";
my $url = "http://www.amazon.com/gp/offer-listing/B00BF9MZ80";
my $response = "";
do {
  print "Sending HTTP req to www.amazon.com\n";
  my $response = `curl -s $url | grep -n -B 30 '$pattern'`;
  print "Response received as:\n$response\n";
  sleep(2);
} until ($response ne "");
print "\nresponse stored.\n";

while循环的原因是有时amazon有时会出现服务器错误。我希望脚本循环,直到它从亚马逊捕获html。脚本输出这样的内容直到我杀了它:

Sending HTTP req to www.amazon.com
Response received as:

Sending HTTP req to www.amazon.com
Response received as:
1687-    
1688-    
1689-
1690-
1691-
1692-
1693-    <span class="a-size-large a-color-price olpOfferPrice a-text-bold">        $132.29    </span>
1694-    <span class="a-color-price">
1695-        
1696-
1697-    </span>
1698-
1699-
1700-        <br>
1701-        <span class="a-color-secondary">
1702:            & <b>FREE Shipping</b>. <a href="/gp/help/customer/display.html?ie=UTF8&nodeId=527692&pop-up=1" target="SuperSaverShipping" onclick="
return amz_js_PopWin('/gp/help/customer/display.html?ie=UTF8&nodeId=527692&pop-up=1','SuperSaverShipping','width=550,height=550,resizable=1,scrollbars
=1,toolbar=0,status=0');">Details</a>
Sending HTTP req to www.amazon.com
Response received as:

Sending HTTP req to www.amazon.com
Response received as:

我也尝试将until()条件更改为until (length($response) > 5)并获得相同的输出。当$response持有html时,有人可以告诉我为什么会这样结束吗?

也许这与存储在数组中的输出有关,但我不完全理解它在Perl中是如何工作的。请帮帮我!

1 个答案:

答案 0 :(得分:3)

因为两个$response变量不同。删除内部my,事情应该开始工作。