13. What are some of the primary differences between Tcl/Tk and Perl/Tk?
Considering that both interpreters/(compiler) for Tcl and Perl were written in C for original use on Unix computers it is not surprising that there are some similarities between the two languages.
Nevertheless, there are a large number of differences between the Tcl language and the Perl language. One thing to keep in mind is that to build, install, and use Perl/Tk one does not need to have Tcl/Tk on hand at all. Perl/Tk is completely independent of Tcl/Tk.
Within each language there is Tk - a widget Toolkit. One must be careful that some of the Tcl/Tk widget names and options have been modified slightly in the perl/Tk language.
With Tk-b9.01 (and higher) a great many functions (method calls actually) start with an upper case letter and continue with all lower case letters (e.g. there is a perl/Tk Entry widget but no entry widget), and many configuration options are all lower case (e.g. there is a perl/Tk highlightthickness option but no highlightThickness option). Thus if you are having trouble converting a script check your typing. (there is a script b9names to help). There is also a tcl2perl script (discussed later).
The html docs that get created during the build of perl/Tk ought to help clarify most any language difference. While the following table does not cover all the differences it is hoped that it will prove useful, especially to those people coming from a primarily Tcl/Tk programming background. These are some of the common Tcl->Perl stumbling points: what Tcl/Tk Perl/Tkvariable set a 123 $a = 123; or $a = '123'; initializationre-assignment set b $a $b = $a;lists/arrays set a {1 2 fred 7.8} @a = (1,2,'fred',7.8);re-assignment list set b $a @b = @a;associative set a(Jan) 456.02 %a = ('Jan',456.02,'Feb',534.96); arrays set a(Feb) 534.96re-assignment foreach i / %b = %a; [array names a] { set b($i) = $a($i) }Note on the above examples:In Tcl the scalar, list, and array variable 'a' will overwrite each previous assignment.In Perl $a, @a, %a are all distinct (occupy separate namespaces).expressions set a [expr $b $c] $a = $b $c;increment incr i $i ; or $i;declare proc plus {a b} { sub plus { my($a,$b) = @_; subroutines expr $a $b } $a $b; }variable scope local default global default override w/ "global" override w/ "my" (or "local")call plus 1 2 &plus(1,2); #or subroutines plus(1,2); #OK after sub plusstatement sep newline or at ";" ";" requiredstatement "/" - newline none required continuationverbatim strings {} '' e.g. {a / lot@ of $stuff} 'a / lot@ of $stuff'escaped strings "" "" e.g. "Who/nWhat/nIdunno" "Who/nWhat/nIdunno"STDOUT puts "Hello World!" print "Hello World!/n" puts stdout "Hello!" print STDOUT "Hello!/n"
Note also that Tcl/Tk has a built-in abbreviation completion mechanism that lets you specify short hand, e.g. canvas .frame.canvas -yscrollcommand ".frame.scroll set" ; #Tcl/Tk OK canvas .frame.canvas -yscroll ".frame.scroll set" ; #Tcl/Tk also OK $canvas=$main->Canvas(-yscroll => ['set',$scroll]); #ERROR perl/Tk $canvas=$main->Canvas(-yscrollcommand => ['set',$scroll]); #perl/Tk OK
You may get around this with the perl abbrev.pl package in certain circumstances. For example: require 'abbrev.pl'; %foo = (); &abbrev(*foo,'-yscrollcommand'); ... $canvas=$main->Canvas($foo{'-yscroll'} => ['set',$scroll]); #perl/Tk OK
In Perl you can emulate the Tcl unknown proc (through the perl AUTOLOAD mechanism) as follows: use Shell; print($p = man(-k => bitmap));
Which is equivalent to what you would get if you typed: man -k bitmap
From within tclsh or wish. (Thanks to Ilya Zakharevich mailto:ilya@math.ohio-state.edu for pointing out this feature. ;-)