Your code looks pretty good. use strit warnings and diagnostics or die is worth a read RE benefits of strict. foreach and for are synonyms. You use aliasing to $_ in one loop and a my $var in another. This is inconsistent but it makes for easier to read code to write for my $whatever (@widgets) {} as your code becomes more self documenting, especially if the contents of @widgets is not immediately apparent. This loop structure:

$num_domains = scalar @domains; while ($num_domains > '0') { # Start Loopy Code [blah] my $new_domain = shift @domains; [blah] $num_domains = scalar @domains; # get the size of array }

is pretty obtuse to me. while loops are fine but they will also go infinite if you don't satisfy the condition (easy to do). All you really need is a simple

for ( 0 .. $#@domains ) { # do stuff } # or for my $root_domain ( @domains ) { # blah push @domains, @new_domains; }

The second code snippet is nifty because if you say extract the links from a page and then add the domains to @domains the @domains list gets longer and....you have an instant spider. Without using recursion per se. You can use this iterating over a list/adding to the list in the loop technique to 'recurse' virtually any structure you like. There is an example at Link Checker FWIW that will spider a website. You can also walk a directory tree in a few lines with:

my $root = 'd:/perl/'; my ( @dirs, @fails, @files); @dirs = ( $root ); for my $dir ( @dirs ) { opendir DIR, $dir or die $!;#do { push @fails, $dir; next }; while ( $_ = readdir DIR ) { next if -s "$dir/$_"; do{ push @dirs, "$dir/$_"; next } if -d "$dir/$_" and not m/^\.\.?$/; do{ push @files, "$dir/$_"; next } if -f "$dir/$_"; } } print "$_\n" for @dirs;

Sure you can use File::Find but this is short, sweet and quite a bit faster.

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print


In reply to Re: Re: (jeffa) Re: References and Arrays by tachyon
in thread References and Arrays by CodeJunkie

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.