in reply to Re^4: WWW::Mechanize problem
in thread WWW::Mechanize problem
my @work = fetch_jobs($starting_condition); while ( @work ) { # ... }
When we evaluate an array in this context, it will be false when the array is empty. The while loop will terminate because all work has been completed. Inside the loop, we add to our known work queue/stack by checking to see if the job leads to more work.
We now need to consider that one job may lead back to itself and break the infinite loop.my @work = fetch_jobs($starting_condition); while ( @work ) { # Remove 1 item from our stack/queue my $job = shift @work; # Possibly add new jobs to our stack/queue if ( more_jobs($job) ) { push @work, fetch_jobs($job); } # process job }
We can additionally decide to abandon our work if we discover that our queue/stack has grown larger than we anticipated. We rely on the fact that when an array is evaluated in scalar context it returns the number of elements present.my @work = fetch_jobs($starting_condition); my %seen; while ( @work ) { # Remove 1 item from our stack/queue my $job = shift @work; # Skip this job if we have already done it next if $seen{$job}++; # Possibly add new jobs to our stack/queue if ( more_jobs($job) ) { push @work, fetch_jobs($job); } # process job }
Now it may be important to process the work in a specific order. A depth first approach is when one job leads to another job which leads to another job and they need to be processed it that order. A breadth first approach is when secondary and tertiary jobs are only executed after all primary jobs are complete. The way to control this is by adjusting what end of the stack/queue you take work off and put on. See push, pop, shift, unshift for more details.my @work = fetch_jobs($starting_condition); my %seen; while ( @work && @work < 1000 ) { # ... }
Cheers - L~R
Update: Minor oversights corrected per the astute herveus.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: WWW::Mechanize problem
by herveus (Prior) on Oct 20, 2005 at 16:56 UTC |