Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Calling procs in Tcl and how it compares to Perl

In languages such as Java, you must call your named blocks of code (methods) with the correct number and type of parameters, but in Perl you may call them with as many or as few parameters as you like ... then write the named block of code (a sub) to handle (or ignore) whatever it gets.

This week, I'm running a Tcl Course in Cambridge for a group of delegates who are already familiar with Perl (and some other languages) and I'm showing them how things differ in Tcl. Here are some of the contrasts ...

1. Since Tcl is a true interpreter, you MUST define your procs earlier in your code than the code that calls it.

2. Since Tcl is a true interpreter, you MAY define your procs conditionally - for example you could have an "if .... else" command, and define something different - but using the same name - depending on a test

3. In Tcl, you can redefine a proc while your script is running - you won't get the "doubly defined" error of "C", and the interpretive nature of the language (as oposed to Perl, which is pseudoscripting only, means you don't have a compile phase at which all procs must be defined.

4. When you declare a proc, you specify the number of parameters - for example
proc cardpack {suites ranks} { ....
is how you would define a proc that takes two parameters

5. You may add a list within a list for optional parameters - for example
proc cardpack {suits {ranks 13} {jokers 0}} { ...
which defines a proc that takes one, two of three parameters - but not zero, nor four or more. If you give just one parameter, it's put into the variable calls $suits and $ranks defaults to 13 and $jokers to 0.

6. If you want to accept an indeterminate number of parameters, you use the special name args for the final parameter, and that acts like a sponge to collect all remaining parameter valuse as a list within the proc - for example
proc hand {firstcard secondcard args} { ...
called with
hand 13 17 75 32 32 44 32
would store 13 in $firstcard, 17 in $secondcard, and all the rest into a list in $args - that's {75 32 32 44 32}.

7. In Tcl you are calling by value, so information passed in to a proc is in effect copied in there, and if you alter it you are not altering the original in the calling code. If you want to alter the value within the proc, you'll need to pass in the variable name and use upvar

8. You may pass arrays in to procs - but you must call by name / use upvar

In Perl, variables default to being our or global, but in Tcl they default to being local. You may declare them global if you like.
(written 2008-09-02, updated 2008-09-04)

 
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)
  [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)
  [4398] Accessing variables across subroutine boundaries - Perl, Python, Java and Tcl - (2015-01-18)

P209 - Subroutines in Perl
  [357] Where do Perl modules load from - (2005-06-24)
  [531] Packages in packages in Perl - (2005-12-16)
  [588] Changing @INC - where Perl loads its modules - (2006-02-02)
  [969] Perl - $_ and @_ - (2006-12-07)
  [1202] Returning multiple values from a function (Perl, PHP, Python) - (2007-05-24)
  [1784] Global - Tcl, PHP, Python - (2008-09-03)
  [1826] Perl - Subs, Chop v Chomp, => v , - (2008-10-08)
  [1850] Daisy the Cow and a Pint of Ginger Beer - (2008-10-21)
  [1860] Seven new intermediate Perl examples - (2008-10-30)
  [1921] Romeo and Julie - (2008-12-04)
  [2069] Efficient calls to subs in Perl - avoid duplication, gain speed - (2009-03-07)
  [2550] Do not copy and paste code - there are much better ways - (2009-12-26)
  [2833] Fresh Perl Teaching Examples - part 2 of 3 - (2010-06-27)
  [3066] Separating groups of variables into namespaces - (2010-11-24)
  [3574] Perl functions such as chop change their input parameters - (2012-01-10)
  [3833] Learning to use existing classes in Perl - (2012-08-10)


Back to
Reception
Previous and next
or
Horse's mouth home
Forward to
Think before you send
Some other Articles
Picturing the rain
What is running on your network? (tcl and expect)
Think before you send
Calling procs in Tcl and how it compares to Perl
Reception
Server overloading - turns out to be feof in PHP
Injection Attacks - avoiding them in your PHP
Pointing all the web pages in a directory at a database
The Rise and Rise of First Bus Fares
4759 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, 96 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., 2024: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.info/mouth/1782_Cal ... -Perl.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb