Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
Accessing variables across subroutine boundaries - Perl, Python, Java and Tcl

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
J709 - Java - Class Access
  [3142] Private and Public - and things between - (2011-01-22)
  [3047] What is a universal superclass? Java / Perl / Python / Other OO languages - (2010-11-13)
  [2535] When should I use Java, Perl, PHP, or Python? - (2009-12-13)
  [2419] Where is my Java class? - (2009-09-24)
  [874] Who can use which access door? - (2006-09-21)

P301 - Variables in Perl
  [4608] Introspecion in Perl 6 - (2016-01-02)
  [3430] Sigils - the characters on the start of variable names in Perl, Ruby and Fortran - (2011-09-10)
  [3059] Object Orientation in an hour and other Perl Lectures - (2010-11-18)
  [2972] Some more advanced Perl examples from a recent course - (2010-09-27)
  [2877] Further more advanced Perl examples - (2010-07-19)
  [2374] Lead characters on Perl variable names - (2009-08-24)
  [2241] Perl references - $$var and \$var notations - (2009-06-15)
  [1946] Variable Types in Perl - (2008-12-15)
  [1581] What is an lvalue? (Perl, C) - (2008-03-18)
  [975] Answering ALL the delegate's Perl questions - (2006-12-09)

Y111 - Python - More on Collections and Sequences
  [4442] Mutable v Immuatble objects in Python, and the implication - (2015-02-24)
  [3797] zip in Python - (2012-07-05)
  [3439] Python for loops - applying a temporary second name to the same object - (2011-09-14)
  [3348] List slices in Python - 2 and 3 values forms, with an uplifting example - (2011-07-06)
  [3150] Python dictionaries - mutable and immutable keys and values - (2011-01-29)
  [2996] Copying - duplicating data, or just adding a name? Perl and Python compared - (2010-10-12)
  [2920] Sorting - naturally, or into a different order - (2010-08-14)
  [2894] Sorting people by their names - (2010-07-29)
  [2718] Python - access to variables in the outer scope - (2010-04-12)
  [1873] List Comprehensions in Python - (2008-11-06)
  [1869] Anonymous functions (lambdas) and map in Python - (2008-11-04)
  [1310] Callbacks - a more complex code sandwich - (2007-08-19)
  [1304] Last elements in a Perl or Python list - (2007-08-16)
  [899] Python - extend v append on a list - (2006-10-20)
  [633] Copying a reference, or cloning - (2006-03-05)
  [386] What is a callback? - (2005-07-22)
  [61] Python is a fabulous language - (2004-09-24)

T207 - Tcl/Tk - Procedures and Variable Scope
  [3629] Sharing lots of values in Tcl without having lots of global declarations - (2012-02-28)
  [3414] Passing back multiple results in Tcl - upvar and uplevel - (2011-09-01)
  [3192] Tcl - Some example of HOW TO in handling data files and formats - (2011-03-04)
  [2929] Passing a variable number of parameters in to a function / method - (2010-08-20)
  [2520] Global and Enable - two misused words! - (2009-11-30)
  [2476] Tcl - uplevel to run code at calling level - (2009-10-24)
  [2466] Tcl - passing arrays and strings in and back out of procs - (2009-10-22)
  [1782] Calling procs in Tcl and how it compares to Perl - (2008-09-02)
  [1404] Tcl - global, upvar and uplevel. - (2007-10-24)
  [1163] A better alternative to cutting and pasting code - (2007-04-26)
  [775] Do not duplicate your code - (2006-06-23)
  [409] Functions and commands with dangerous names - (2005-08-11)
  [308] Call by name v call by value - (2005-05-11)
  [122] Passing arrays to procs in Tcl - (2004-11-18)
  [96] Variable Scope - (2004-10-22)


Back to
TransWilts / Press and Publicity report for AGM / 30th January 2015
Previous and next
or
Horse's mouth home
Forward to
The Campus in Melksham - name, logo, and livery choice
Some other Articles
Finding sum, minimum, maximum and average in Python (and Ruby)
Selecting RECENT and POPULAR news and trends for your web site users
Commenting out an echo killed my bash backup script
The Campus in Melksham - name, logo, and livery choice
Accessing variables across subroutine boundaries - Perl, Python, Java and Tcl
TransWilts / Press and Publicity report for AGM / 30th January 2015
Java Utility class - flexible replacement for array. Also cacheing in objects and multiple catch clauses example.
Preparing data through a little bit of Perl
Philosophy behind object design - and how I applied in to a Java example
Splitting Java strings and extracting numbers
4728 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2017: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/4398_Acc ... d-Tcl.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat