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 }