my @work = fetch_jobs($starting_condition); while ( @work ) { # ... } #### 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 } #### 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 } #### my @work = fetch_jobs($starting_condition); my %seen; while ( @work && @work < 1000 ) { # ... }