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.


In reply to Re: From single script to code reuse by haukex
in thread From single script to code reuse by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.