I'm really having a hard time putting responses where they need to be. Since my re-working of what aaron posted and what AM posted are catching in the same place, I hope that putting the responses here doesn't offend.
I had two big problems in the original post that have now been corrected: single quotes around the zero for false and calling for a new DirPP while in the loop that downloads the images.
Now I'm trying to integrate the slicker versions of DirPP and stumble on the same error despite which version I try:
$ perl tg2.pl Unrecognized LWP::UserAgent options: autodie at tg2.pl line 7 Use of uninitialized value $dir in string at tg2.pl line 16. Use of uninitialized value $dir in string at tg2.pl line 16. ... Use of uninitialized value $dir in string at tg2.pl line 16. ^C $ cat tg3.pl #!/usr/bin/perl -w use strict; use WWW::Mechanize; use LWP::Simple; use feature qw/ state /; my $domain = 'http://www.yahoo.com'; my $m = WWW::Mechanize->new( qw/ autodie 1 /); $m->get( $domain); my $counter = 0; my @list = $m->images(); my $dir = &DirPP; for my $img (@list) { my $url = $img->url_abs(); $counter++; my $filename = "$dir". "/image_". "$counter"; #line 16 getstore($url,$filename) or die "Can't download '$url': $@\n"; } sub DirPP { use Errno qw/ EACCES /; # permission denied my $word = "site"; my $counter = 1; my $name ; my $made = 0; while(1){ $name = sprintf '%s%3d', $word, $counter; last if not $made = mkdir $name, 0755 ; die $! if $! == EACCES; } return $name if $made; return; } $
From the look of the output, the subroutine is being called many times, but to my eye, I call it only once, and that is before I enter the loop that assigns a value to $dir . (scratches head) That was aaron's version, but I induce the same behavior with AM's version:
$ perl tg2.pl Unrecognized LWP::UserAgent options: autodie at tg2.pl line 7 Use of uninitialized value $dir in string at tg2.pl line 15. Use of uninitialized value $dir in string at tg2.pl line 15. Use of uninitialized value $dir in string at tg2.pl line 15. Use of uninitialized value $dir in string at tg2.pl line 15. Use of uninitialized value $dir in string at tg2.pl line 15. Use of uninitialized value $dir in string at tg2.pl line 15. Use of uninitialized value $dir in string at tg2.pl line 15. Use of uninitialized value $dir in string at tg2.pl line 15. ^C $ cat tg2.pl #!/usr/bin/perl -w use strict; use WWW::Mechanize; use LWP::Simple; use feature ':5.10'; my $domain = 'http://www.yahoo.com'; my $m = WWW::Mechanize->new( qw/ autodie 1 /); $m->get( $domain); my @list = $m->images(); my $counter = 0; my $dir = &DirPP; 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"; } sub DirPP { use Errno qw/ EACCES /; # permission denied state $counter2 = 1; my $word = "site"; my $name ; my $made = 0; while(1){ $name = sprintf '%s%3d', $word, $counter2; last if not $made = mkdir $name, 0755 ; die $! if $! == EACCES; } return $name if $made; return; } $
So, I'm stumped and out of guesses. That means I have no excuse to stay on the computer...off to the world of work..., happy tuesday,
In reply to Re^3: getting a while loop to terminate
by Aldebaran
in thread getting a while loop to terminate
by Aldebaran
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |