Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: getting a while loop to terminate

by thezip (Vicar)
on Apr 15, 2012 at 03:55 UTC ( [id://965121]=note: print w/replies, xml ) Need Help??


in reply to getting a while loop to terminate

It's because you have set $flag to '0', which is an uninterpolated string containing the character '0', a true value.

Instead, you should set $flag to zero, (0), without the single quotes. This will evaluate to false and make your logic all happy.


What can be asserted without proof can be dismissed without proof. - Christopher Hitchens, 1949-2011

Replies are listed 'Best First'.
Re^2: getting a while loop to terminate
by Anonymous Monk on Apr 15, 2012 at 04:20 UTC

      I guess I don't follow you here:

      $ perl -le " die 666 if q/0/ " $ perl -le " die 666 if $what = q/0/ " syntax error at -e line 1, near "if =" Execution of -e aborted due to compilation errors. $

        My shell doesn't interpolate $what, as you can see from the session I posted, use single quotes if your shell interpolates doubles

        Or you could even omit  $what =

        Whether it is assigned to a variable or not , '0' is always false

Re^2: getting a while loop to terminate
by Aldebaran (Curate) on Apr 17, 2012 at 16:41 UTC

    Yay! first taste of victory....

    $ perl tg1.pl site_13 $ cd site_13 $ ls image_1 image_2 image_3 ... $ cd .. $ 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 $dir = &dirname; my @list = $m->images(); 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 dirname { my $word = "site"; my $counter = 1; my $flag = 1; while ($flag) { my $name = "$word" . "_" . "$counter"; if ( -d $name) { $counter++; next; } else { mkdir $name, 0755 or warn "Cannot make dir $name: $!"; $flag = 0; } print "$name \n"; return $name; } } $

    I have a whole bunch of questions at this point, but don't want to ask them all. Let me ask this one, because it goes to readability: Is there an indent command that works well for perl? indent -i2 foo.pl was not a winner.

      You may be looking for Perl::Tidy which does an excellent job of pretty printing Perl and is highly configurable.

      True laziness is hard work

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://965121]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-04-19 22:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found