Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re: How to localize conditionally?

by ELISHEVA (Prior)
on Mar 14, 2011 at 05:13 UTC ( #893027=note: print w/replies, xml ) Need Help??

in reply to How to localize conditionally?

Personally, I'd go with JavaFan's solution if you only have one environment variable and autovivification "risk" is not a concern to you (see replies to JavaFan's response above). I like the way it keeps the impact of the condition clearly focused on the variable and also makes clear the possible values.

Although this code

if( condition() ) { local $ENV{BLAH} = 42; libcall(); } else { libcall(); }

is overkill for a single state variable, you might want to consider it later on, if it turns out that you have to set up multiple state all tied to the same condition. Were you to avoid the "if", and repeat the condition for each environment variable you need to set, you might well have a code duplication problem.

On the other hand, I'd be hard pressed to call a single statement containing a parameterless subroutine call "code duplication". Code duplication (don't repeat yourself) typically applies to expressions with "movable parts": a single call with a long parameter list, multiple expressions passed as parameters, a conditional assignments ($x= COND ? A : B)or multiple lines of code. In these cases replication is problematic because multiple expressions and statements with identical internal logical are repeated. If the internal logic in one is buggy, all will be buggy and need to be fixed.

However, a single parameterless function call has no internal moving parts. There are no "debug everywhere issues". Either you named the function correctly in a particular location or you didn't and that is about the limit of the debugging effort.

Update: added comment about autovivification risk.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://893027]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (12)
As of 2017-06-23 18:19 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (554 votes). Check out past polls.