while (@found < $pics_to_find * 30) { last unless @found_galleries; # exit the loop if there are no more my $gallery_content = get(pop @found_galleries); while ($gallery_content =~ /.../) { ... # why use "yes" and "no"? why not just a true value or a false value? next if $limit_img_size eq "yes" and !some_size_constraint(); push @found, ...; } }