Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: A cleaner way of scoping variables

by Chady (Priest)
on Aug 09, 2004 at 13:26 UTC ( #381218=note: print w/ replies, xml ) Need Help??


in reply to A cleaner way of scoping variables

If $bar is temporary, and your main worry is about ending up declaring a lot of globals for temporary usage, then consider changing your code.

I know that your example is simplified for the sake of posting, but nevertheless, in your example you can throw away $bar completely.

if ($foo) { print "true"; }

But if you want to use the value a little farther below it probably means that you need that variable, and hence you should declare it, or maybe you should group together the parts of code that are working on the same temporary variable so that you can scope tightly.

just my 2 cents.


He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

Chady | http://chady.net/


Comment on Re: A cleaner way of scoping variables
Download Code
Re^2: A cleaner way of scoping variables
by KeighleHawk (Beadle) on Aug 10, 2004 at 23:43 UTC
    Another consideration along this lines and that of using subroutines is general code style/design.

    If you break your code up more into subrotines, methods, modules, classes etc. you still have the problem though it is considerably less relevent.

    i.e. if your whole code block (sub/method) is already naturally defined as 10-20 lines of code, then the fact $bar is scoped slightly longer than you want, hardly matters since it is going to go right out of scope a few lines later anyway when your subroutine/method ends.

    I know my preference may vary from others, but when I get my code reduced to classes and a "driver" script that just says:

    do_step_1
    do_step_2
    do_step_3
    exit_gracefully

    I am quite happy with it. Debugging generally very quickly gets you to 10-20 lines of code making it much easier to figure out. Then, if you are using global variables in this scenario, you deserve whatever you get...

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2014-12-29 04:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (184 votes), past polls