Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Flag variables

by adrianh (Chancellor)
on Mar 03, 2003 at 13:49 UTC ( [id://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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2024-04-23 09:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found