Hello good people
I've glanced at other people's posts and must say that this is very low-brow in comparison to what others are able to do. I guess we all start somewhere. Most of this script works for me, but I haven't quite wrangles the subroutine to return a new directory, in particular, what happens is that the while loop never terminates:
$ perl tg1.pl
Useless use of private variable in void context at tg1.pl line 33.
site_138
site_139
site_140
site_141
^C
$ cat tg1.pl
#!/usr/bin/perl -w
use strict;
use WWW::Mechanize;
use LWP::Simple;
my $domain = 'http://www.yahoo.com';
my $m = WWW::Mechanize->new;
$m->get( $domain);
my $counter = 0;
my @list = $m->images();
for my $img (@list) {
my $url = $img->url_abs();
$counter++;
my $dir = &dirname;
my $filename = "$dir". "/image_". "$counter";
getstore($url,$filename) or die "Can't download '$url': $@\n";
}
sub dirname {
my $word = "site";
my $counter = 1;
my $flag = 1;
while ($flag) {
my $name = "$word" . "_" . "$counter";
if ( -e -d $name) {
$counter++;
next;
}
else {
mkdir $name, 0755 or warn "Cannot make dir $name: $!";
$flag = '0';
}
print "$name \n";
$name;
}
}
$
I know that execution goes through the else clause because I have dozens of directories now. I believe that I have a correct form for making the $flag false in the same clause, but perl thinks that I do nothing to end this control. How come?
Also accepting any tips to make this more concise and readable. Thanks in advance,
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|