Ouch, this hurts! Usually this line indicates a deamon which is never
supposed to terminate.
Maybe. Personally I never use while (1), I prefer for (;
![Wink ;) ;)](data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7)
instead.
But neither implies that the loop is really infinite: It may (and often
is) be left with last or return.
Why not move the loop condition into the loop condition?
my $page = get "$pbase?page=$pcnt&pid=$pid";
while ((!$page =~/No sorties/) and (!$page =~/"sid=$lproc"/)) {
append_file( "c:/scr/$pid.txt", $page );
$pcnt++;
$page = get "$pbase?page=$pcnt&pid=$pid";}
}
Yes, I know the condition could be formulated better, but I transformed
it as little as possible to demonstrate how the exit() cond can
trivially be moved into the while() cond.
You also demonstrated how easy it is to inadvertently change the logic
in the process. Thomas' code exited *after* saving the page containing
sid=$lproc, yours exits *before* saving it. I don't know which one is
correct, but they are definitely not the same.
I also strongly disagree that it is a good thing to transform a loop of
the form
for (;
![Wink ;) ;)](data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7)
{
get data;
last unless condition;
process data;
}
into
get data;
while (condition) {
process data;
get data;
}
It duplicates the "get data" part which makes it easy to introduce bugs
by only changing one of the parts. Perl has "next" and "last" for a
reason. Use them.
In many cases both the duplication and the use of "last" can be avoided
by using a function call:
sub get_data {
get data;
return condition;
}
while (get_data()) {
process data;
}
But that doesn't always work. Among the nice properties of last and next
is that they can be easily cascaded:
while (get_data()) {
next unless condition1;
process data;
next unless condition2;
process data some more;
next unless condition3;
a little more and we are done;
}
BTW: space characters are very cheap, I just saw a them on sale at
Costco. Fell free to use as many as you like to make your code more
readable.
Full ACK.
hp