In a perfect programming world, code is broken into named blocks (methods, functions, subroutines, classes, procedures, commands, modules, packages etc) and data is passed around between the blocks purely as parameters and return values. And variables are local to the block in which they're used. This means that each block is independent testable and makes for robust and re-usable code.
It's not a perfect programming world, though!
Most languages have the concept of global variables (which you should use with extreme care!).
Tcl offers you
uplevel and
upvar which let you work in your calling (or a higher) namespace from within a
proc (
and it's a vital part of the language too) and there are a number of other ways that elements can be shared. Example -
[here].
In Python, variables can be read (read only) within a method from the surrounding / calling scope - very useful for passing in . For example (from
[here]), the following code
def bynumber (home, away):
fred = cmp(count[home],count[away])
if fred == 0: fred = cmp(home,away)
return fred
In which the
count array is read in the sort function, even though it's not global
In Java inner classes to a little of the same - see
[here] - although in truth that's more one class shared within another.
In Perl variable default to being global (yuk!) and indeed we usually write
use strict;
on all but the shortest of programs and top level codes in order to reverse this design . You can use
my to request variables to be scoped to the block in which they are defined (and note that for the purpose of this article,
they are visible within inner subroutines too), and you can use
our to request a variable has the global scope, even if you are being strict. The real purpose of strict is to avoid the accidental / careless / thoughless creation of global variables, not to stop you doing it in those few right cases. Example of Perl's scoping inc
(written 2015-01-18)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
T207 - Tcl/Tk - Procedures and Variable Scope [96] Variable Scope - (2004-10-22)
[122] Passing arrays to procs in Tcl - (2004-11-18)
[308] Call by name v call by value - (2005-05-11)
[409] Functions and commands with dangerous names - (2005-08-11)
[775] Do not duplicate your code - (2006-06-23)
[1163] A better alternative to cutting and pasting code - (2007-04-26)
[1404] Tcl - global, upvar and uplevel. - (2007-10-24)
[1782] Calling procs in Tcl and how it compares to Perl - (2008-09-02)
[2466] Tcl - passing arrays and strings in and back out of procs - (2009-10-22)
[2476] Tcl - uplevel to run code at calling level - (2009-10-24)
[2520] Global and Enable - two misused words! - (2009-11-30)
[2929] Passing a variable number of parameters in to a function / method - (2010-08-20)
[3192] Tcl - Some example of HOW TO in handling data files and formats - (2011-03-04)
[3414] Passing back multiple results in Tcl - upvar and uplevel - (2011-09-01)
[3629] Sharing lots of values in Tcl without having lots of global declarations - (2012-02-28)
Y111 - Python - More on Collections and Sequences [61] Python is a fabulous language - (2004-09-24)
[386] What is a callback? - (2005-07-22)
[633] Copying a reference, or cloning - (2006-03-05)
[899] Python - extend v append on a list - (2006-10-20)
[1304] Last elements in a Perl or Python list - (2007-08-16)
[1310] Callbacks - a more complex code sandwich - (2007-08-19)
[1869] Anonymous functions (lambdas) and map in Python - (2008-11-04)
[1873] List Comprehensions in Python - (2008-11-06)
[2718] Python - access to variables in the outer scope - (2010-04-12)
[2894] Sorting people by their names - (2010-07-29)
[2920] Sorting - naturally, or into a different order - (2010-08-14)
[2996] Copying - duplicating data, or just adding a name? Perl and Python compared - (2010-10-12)
[3150] Python dictionaries - mutable and immutable keys and values - (2011-01-29)
[3348] List slices in Python - 2 and 3 values forms, with an uplifting example - (2011-07-06)
[3439] Python for loops - applying a temporary second name to the same object - (2011-09-14)
[3797] zip in Python - (2012-07-05)
[4442] Mutable v Immuatble objects in Python, and the implication - (2015-02-24)
P301 - Variables in Perl [975] Answering ALL the delegate's Perl questions - (2006-12-09)
[1581] What is an lvalue? (Perl, C) - (2008-03-18)
[1946] Variable Types in Perl - (2008-12-15)
[2241] Perl references - $$var and \$var notations - (2009-06-15)
[2374] Lead characters on Perl variable names - (2009-08-24)
[2877] Further more advanced Perl examples - (2010-07-19)
[2972] Some more advanced Perl examples from a recent course - (2010-09-27)
[3059] Object Orientation in an hour and other Perl Lectures - (2010-11-18)
[3430] Sigils - the characters on the start of variable names in Perl, Ruby and Fortran - (2011-09-10)
[4608] Introspecion in Perl 6 - (2016-01-02)
J709 - Java - Class Access [874] Who can use which access door? - (2006-09-21)
[2419] Where is my Java class? - (2009-09-24)
[2535] When should I use Java, Perl, PHP, or Python? - (2009-12-13)
[3047] What is a universal superclass? Java / Perl / Python / Other OO languages - (2010-11-13)
[3142] Private and Public - and things between - (2011-01-22)
Some other Articles
Finding sum, minimum, maximum and average in Python (and Ruby)Selecting RECENT and POPULAR news and trends for your web site usersCommenting out an echo killed my bash backup scriptThe Campus in Melksham - name, logo, and livery choiceAccessing variables across subroutine boundaries - Perl, Python, Java and TclTransWilts / Press and Publicity report for AGM / 30th January 2015Java Utility class - flexible replacement for array. Also cacheing in objects and multiple catch clauses example.Preparing data through a little bit of PerlPhilosophy behind object design - and how I applied in to a Java exampleSplitting Java strings and extracting numbers