for (my @stack = keys %links; $#stack>=0; ) { my $current = shift @stack; next if $links{$current}{visited}; if (something_happens()) { push @stack, "another key"; } }