Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Flag variables

by adrianh (Chancellor)
on Mar 03, 2003 at 13:49 UTC ( #240023=note: print w/ replies, xml ) Need Help??


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.


Comment on Re: Flag variables
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://240023]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (7)
As of 2014-04-18 00:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (460 votes), past polls