Here is the code with the above suggestions applied.

#!/usr/bin/perl use strict; use warnings; use LWP::UserAgent; use HTML::LinkExtor; use URI::URL; my $site = shift @ARGV; # Why not just take an uri as argument? my $domain = shift @ARGV; # Should really be an uri. # Assume it's a HTTP server if not. if ($site !~ /^\w+:/) { $site = "http://site"; } my $first_uri = URI->new($site)->canonical; my @to_visit = ( $first_uri ); my %seen = map { $_ => 1 } @to_visit; my $ua = LWP::UserAgent->new(); while (@to_visit) { my $uri = shift(@to_visit); # Make the parser my $p = HTML::LinkExtor->new(sub { my ($tag, %attr) = @_; # Only interested in A elements. return if $tag ne 'a'; # Only interested in the HREF attribute. return if not exists $attr{href}; my $link_uri = URI->new_abs($attr{href}, $uri)->canonical; # Ignore links outside of the domain. return if $link_uri->rel($first_uri) eq $link_uri; # Ignore links already in the queue and links already visited. return if $seen{$link_uri}++; push @to_visit, $link_uri; }); my $response = $ua->request( HTTP::Request->new(GET => $uri), sub { $p->parse($_[0]) } ); $p->eof; } my @links = keys %seen;

You should use LWP::RobotUA instead of LWP::UserAgent for this kind of application.

Untested.

Updated: The call to request was accidently removed! oops. Readded. Added $p->eof.


In reply to Re^2: Newbie memory leak by ikegami
in thread Newbie memory leak by memwaster

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.