I thought I posted the final version of this. I've been reading as I come here and noticed that it's a possibility that if your thread dithers on too long, someone might just cut it off. I don't know whether that happened, or I just forgot to click the create button, but I don't think a thread has finished until one has a version with a bit of polish:

$ perl tg6.pl downloaded 83 images from http://www.yahoo.com to folder site_4 $ cat tg6.pl #!/usr/bin/perl -w # creates a new directory and downloads images from url to it # perlmonks node 965537; thx aaron and A.M. use strict; use feature ':5.10'; use WWW::Mechanize; use LWP::Simple; use Errno qw[ EEXIST ]; # get information about images my $domain = 'http://www.yahoo.com'; my $m = WWW::Mechanize->new(); $m->get( $domain); my @list = $m->images(); # create new folder and download images to it. my $counter = 0; my $dir = &mk_new_dir; for my $img (@list) { my $url = $img->url_abs(); $counter++; my $filename = $dir. "/image_". $counter; getstore($url,$filename) or die "Can't download '$url': $@\n"; } # output print "downloaded ", $counter, " images from ", $domain, "\n"; print "to folder ", $dir, "\n"; sub mk_new_dir { my $counter2 = 1; while(1){ my $word = "site"; my $name = $word . '_' . $counter2++; if( mkdir $name, 0755 ){ return $name; # success, return new dir name } else { next if $!{EEXIST}; # mkdir failed because the file exists die $!; # it failed for some other reason; bail out! } } $

I think aaron's integrating the 'mkdir $name, 0755' as the truth condition of the sub is great, as well as the post-increment of $counter2 to drive the thing forward.

}

In reply to Re^8: getting a while loop to terminate by Aldebaran
in thread getting a while loop to terminate by Aldebaran

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.