What API you provide depends a bit on what kind of data you want to return. It looks like you're scraping links, so for example, you could have one part of your code return those links as a Perl data structure, which you can then feed into other parts of the code. In that respect, perlreftut and perldsc are probably good reads. Here's just one quick untested idea, and remember, TIMTOWTDI.
sub getlinks {
my ($mech,...) = @_;
... code to get the webpage here ...
my @links;
for my $result ( $mech->links() ) {
push @links, { title=>$result->name,
text=>$result->text };
}
return \@links;
}
In general, for modularizing code, depending on what level you're starting at, there's perlsub, perlmod, and perlootut, or the book Modern Perl also has chapters on modules and OO.
As for the general question in the title, see e.g. Moving from scripting to programming.
|