【问题标题】:How to use a perl module that you have written?如何使用您编写的 perl 模块?
【发布时间】:2010-12-29 03:34:10
【问题描述】:

我刚刚编写了我的第一个 Perl 模块,但无法让它与我也制作的脚本一起工作。这是当我尝试运行正在使用我新创建的模块的脚本时 Perl 解释器显示的错误。

错误信息:

scraper_tools_v1.pm did not return a true value at getYid.pl line 5.
BEGIN failed--compilation aborted at getYid.pl line 5.

scraper_tools_v1.pm 是我编写的 Perl 模块,getYid.pl 是尝试利用 scraper_tools_v1.pm 模块的 Perl 脚本。

这里是 scraper_tools_v1.pm 文件的代码:

#!/usr/bin/perl

package scraper_tools_v1;

use strict;
use warnings;
use WWW::Curl::Easy;

# Note this function expects a single parameter which should be in the form of a URL

  sub getWebPage($)
  {
    # Setting up the Curl parameters
    my $curl = WWW::Curl::Easy->new; # create a variable to store the curl object

    # A parameter set to 1 tells the library to include the header in the body output.
    # This is only relevant for protocols that actually have headers preceding the data (like HTTP).
    $curl->setopt(CURLOPT_HEADER, 1);

    # Setting the target URL to retrieve with the passed parameter
    $curl->setopt(CURLOPT_URL, @_);

    # Declaring a variable to store the response from the Curl request
    my $response_body = '';

    # Creating a file handle for CURL to output to, then redirecting our output to the $response_body variable
    open(my $fileb, ">",\$response_body) or die $!;
    $curl->setopt(CURLOPT_WRITEDATA, $fileb);

    # getting the return code from the header to see if the GET was successful
    my $return_code = $curl->perform;

    # capturing the response code from the GET request in the HTTP header, i.e... 200, 404, 500, etc...
    # 200 is success
    my $response_code = $curl->getinfo(CURLINFO_HTTP_CODE);  

    # if the return code is zero than the request was a success
    if ($return_code == 0)
    {    
      # A little debug output to keep you informed
      print ("Success ". $response_code.": ".@_."\n");

      # return whatever was contained on the web page that we just got using a GET
      return $response_body;
    }

    else
    {
      print ("Failure ". $response_code.": ".@_."\n");
    }

    close($fileb); # close the file-handle

    }

这里是尝试使用上述模块的 getYid.pl 脚本

#!/usr/bin/perl

use strict;
use warnings;
use scraper_tools_v1;

my %cat_links; # Hash that stores categories and their numbers (ID's)
my $web_page = scraper_tools_v1->getWebPage("http://something.com/categoryindex.aspx");

my @lines = split(/\n/, $web_page);

foreach my $line (@lines)
{
  chomp($line);

  if ($line =~ /<option value=\"{1}(.+)\">(.+)<\/option>/)
  {
    my $num = $1;
    my $desc = $2;
    $desc =~ s/\s+&amp;\s+/ & /;
    $cat_links{$desc} = $num;
  }
}

my @allTargetUrls; # make a new array to store all the links we need to extract listings from
$web_page = '';    # Reset this variable so we can reuse it.

my $totalNumberOfListings = 0;

foreach my $key (keys %cat_links)
{
  my $target = "http://something.com/categorydetail.aspx?id=$cat_links{$key}&exact_phrase=0";
  $web_page = scraper_tools_v1->getWebPage($target);

  @lines = split(/\n/, $web_page);

  foreach my $line (@lines)
  {
    my $pages;
    chomp($line);
    if ($line =~ /We found (\d) listings for your search\./)
    {
          my $listingsInCat = $1;
      print ("$cat_links{$key}, $listingsInCat");
      $totalNumberOfListings += $listingsInCat;
    }
    if ($line =~ /Page 1 of (\d)/)
    {
       $pages = $1;
    }

    for (my $i = 1; $i <= $pages; $i++)
    {
      #build the target urls
      my $pageUrl = "http://something.com/categorydetail.aspx?id=$key&search=&exact_phrase=True&city=&state=&zipcode=&page=$i";
      push(@allTargetUrls, $pageUrl);
    }
  }

  print("Total number of listings = ".$totalNumberOfListings);
} 

对于解决此问题的任何帮助将不胜感激,请注意,我已经独立测试了这两个文件的解释器错误,但一无所获。感谢大家观看。

【问题讨论】:

    标签: perl perl-module


    【解决方案1】:

    当你编写一个 Perl 模块时,你应该总是以该行结束文件

    1;
    

    当导入模块时,Perl 在模块级别执行代码。如果您不返回真值(1 为真),那么您将收到您描述的错误。本质上,Perl 是在通知您模块中的初始化代码没有成功。

    【讨论】:

    • @Greg Hewgill 感谢您的评论。我添加了 1;在模块的末尾,但仍然遇到相同的错误。还有其他想法吗?
    • 您确定您正在运行您打算运行的代码吗?你把它保存在你的编辑器里了吗?你是从不同的目录运行的吗?我刚刚在我的系统上尝试了你的代码并添加了1; 解决了这个问题。
    • perl 5.12.2 给出了警告:Argument "http://something.com/categoryindex.aspx" isn't numeric in subroutine entry at scraper_tools_v1.pm line 21。除此之外,它会正确打印出失败消息。你确定你正确使用了原型吗?
    • 对了,先生……我好像迷失在一些沿途创建的临时文件中……我很高兴你指出了这一点,并为我没有抓住它而感到羞愧。
    猜你喜欢
    • 2011-07-07
    • 2020-02-15
    • 2013-02-04
    • 2011-09-23
    • 1970-01-01
    • 1970-01-01
    • 2011-08-03
    • 2017-07-25
    • 1970-01-01
    相关资源
    最近更新 更多