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

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

by Rhys (Pilgrim)
on Sep 14, 2004 at 22:52 UTC ( [id://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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://391003]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-04-23 20:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found