Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

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

Wassercrats,

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 SNMP.pm. 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).

--J

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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2020-10-28 20:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (264 votes). Check out past polls.

    Notices?