http://www.perlmonks.org?node_id=240023


in reply to Flag variables

The "problem" is the gap between where the flag is set and where it is used. This action at a distance can make code far harder to understand and maintain. Code rewrites can move the action a long way from the condition that sets the flag making things even worse. You can very quickly end up with a mess of flags and conditions sprinkled throughout your code.

(as a separate style point I would not use "eq" to test for a boolean flag, just do someaction() if $flag;. Also use of & to call subroutines tends to be frowned upon because of the implicit argument passing.)

If the condition was cheap, I'd just use grep as blakem did.

If the condition is expensive I'd use first from List::Util, which allows you to say:

someaction() if first { $_ eq "foo } @somearray

This keeps the trigger condition and action close together, and doesn't do unnecessary checks once a match is found.