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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |