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)
Some other Articles
Picturing the rainWhat is running on your network? (tcl and expect)Think before you sendCalling procs in Tcl and how it compares to PerlReceptionServer overloading - turns out to be feof in PHPInjection Attacks - avoiding them in your PHPPointing all the web pages in a directory at a databaseThe Rise and Rise of First Bus Fares