Thx aaron and AM, I've got versions working now that are pretty close to what you prescribed:

$ perl tg4.pl site_1 site_2 site_3 site_4 site_5 site_6 site_7 site_8 site_9 site_10 site_11 site_12 site_13 site_14 site_14 $ cd site_14 $ ls image_1 image_2 image_3 image_4 image_5 image_6 image_7 +image_8 ... $ cd .. $ cat tg4.pl #!/usr/bin/perl -w use strict; use WWW::Mechanize; use LWP::Simple; use feature ':5.10'; # 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 = &DirPP; for my $img (@list) { my $url = $img->url_abs(); $counter++; my $filename = "$dir". "/image_". "$counter"; #line 15 getstore($url,$filename) or die "Can't download '$url': $@\n"; } sub DirPP { use Errno qw/ EACCES /; # permission denied my $word = "site"; my $counter2 = 1; my $name; my $made = 0; while(1){ $name = "$word" . "_" . "$counter2"; $counter2++; print "$name \n"; last if $made = mkdir $name, 0755 ; die $! if $! == EACCES; } print "$name \n"; return $name if $made; return; }

Again, I'm looking for tips on style or cautions for robustness. Since one can now demonstrate two ways to do this task, reasonable people could disagree on the final touches, and the discussion might help me to understand these routines better. Here's the one that uses a state variable. Do I use it correctly?

$ perl tg5.pl site_1 site_2 site_3 site_4 site_5 site_6 site_7 site_8 site_9 site_10 site_11 site_12 site_13 site_14 site_15 site_16 site_16 $ cat tg5.pl #!/usr/bin/perl -w use strict; use WWW::Mechanize; use LWP::Simple; use feature ':5.10'; use feature qw/ state /; state $counter3 = 1; # 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 = &DirPP; for my $img (@list) { my $url = $img->url_abs(); $counter++; my $filename = "$dir". "/image_". "$counter"; #line 15 getstore($url,$filename) or die "Can't download '$url': $@\n"; } sub DirPP { use Errno qw/ EACCES /; # permission denied my $word = "site"; my $name; my $made = 0; while(1){ $name = "$word" . "_" . "$counter3"; $counter3++; print "$name \n"; last if $made = mkdir $name, 0755 ; die $! if $! == EACCES; } print "$name \n"; return $name if $made; return; }

In reply to Re^5: 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.