Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re^4: Global variable vs passing variable from sub to sub

by Rhys (Pilgrim)
on Sep 14, 2004 at 22:52 UTC ( #391003=note: print w/replies, xml ) Need Help??

in reply to Re^3: Global variable vs passing variable from sub to sub
in thread Global variable vs passing variable from sub to sub


While I agree that debugging code - particularly code that you're still familiar with - is usually just as easy/difficult with or without globals, the Real Reason that I have discovered that people scope things and use 'my' are as follows:

People use 'my' along with 'use strict' to make sure typos don't get taken seriously. Auto-vivification is cool in some contexts, but I just plain don't see the difference between $options{debug} and $optoins{debug}. Perl does, though, if %options is declared and I have 'use strict' enabled. This has saved me hours of debugging time chasing typos.

People use local variables in subs because they like to turn them into modules that get used in other programs, and sometimes those other programs already have another variable - used for something else - by the same name. I always use $mib, $sess, $var, $vb, and $vl in my SNMP-related scripts. Straight from the man page for This would be a problem if I didn't do a lot of my code in modules (along with 'use strict', 'my', etc. etc.).

I don't find that having local vs. global variables has helped my debugging any, though (not that I use proper debugging techniques, AFAIK).


Replies are listed 'Best First'.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2021-03-04 19:08 GMT
Find Nodes?
    Voting Booth?
    My favorite kind of desktop background is:

    Results (107 votes). Check out past polls.