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

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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.


In reply to Re: How to localize conditionally? by ELISHEVA
in thread How to localize conditionally? by saintmike

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2021-10-27 01:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (91 votes). Check out past polls.

    Notices?