in reply to Loop once on condition 1, many times on cond. 2?

I'm a worry-wart about later needed changes. So rather than going for terser, I'd write:
if ($x eq 'key_1') { DoaBunchOfStuff( $x ); } else { foreach $key (keys %my_hash) { DoaBunchOfStuff( $key ); } }

Replies are listed 'Best First'.
Re^2: Loop once on condition 1, many times on cond. 2?
by GrandFather (Saint) on Oct 09, 2008 at 20:50 UTC

    In that case you should choose one of the other options so that you don't have to make changes to the call to DoaBunchOfStuff in multiple places - changing the parameter list for example. Repeating the "body" in that fashion also makes it less clear from the flow of the code that the two pieces of controlled work are in fact the same.

    In particular, constructing the list to process up front makes it very clear where the processing differences are and what conditions those differences depend on (AnomalousMonk's solution for example).


    Perl reduces RSI - it saves typing