I just wrote my first Perl module, and I was not able to get it to work with the script I created. Here is the error displayed by the Perl interpreter when I try to run a script that uses my newly created module.
Error message:
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 is the Perl module I wrote, and getYid.pl is the Perl script that tries to use the scraper_tools_v1.pm module.
Here is the code for the scraper_tools_v1.pm file:
package scraper_tools_v1;
use strict;
use warnings;
use WWW::Curl::Easy;
sub getWebPage($)
{
my $curl = WWW::Curl::Easy->new;
$curl->setopt(CURLOPT_HEADER, 1);
$curl->setopt(CURLOPT_URL, @_);
my $response_body = '';
open(my $fileb, ">",\$response_body) or die $!;
$curl->setopt(CURLOPT_WRITEDATA, $fileb);
my $return_code = $curl->perform;
my $response_code = $curl->getinfo(CURLINFO_HTTP_CODE);
if ($return_code == 0)
{
print ("Success ". $response_code.": ".@_."\n");
return $response_body;
}
else
{
print ("Failure ". $response_code.": ".@_."\n");
}
close($fileb);
}
And here is a getYid.pl script that tries to use the above module
use strict;
use warnings;
use scraper_tools_v1;
my %cat_links;
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+&\s+/ & /;
$cat_links{$desc} = $num;
}
}
my @allTargetUrls;
$web_page = '';
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++)
{
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);
}
Any help in solving this problem would be greatly appreciated and note that I myself tested both files for interpreter errors and did not find anything. Thanks to everyone for watching.
source