Considering that both interpreter/compilers for Tcl and Perl were written in C for 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 scripting language and the Perl scripting language. Indeed, some of the Tk widget names and options have been modified slightly in the perl/Tk language. With Tk-b9.01 (and higher) a great many functions (methods) 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.
While this 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 background. These are some of the common Tcl->Perl stumbling points:
what Tcl/Tk Perl/Tk
variable set a 123 $a = 123; or $a = '123';
initialization
re-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.96
re-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 plus
statement sep newline or at ";" ";" required
statement "\" - newline none required
continuation
verbatim 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 OKYou 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
<ilya@math.ohio-state.edu>
for pointing out this feature. ;-)
Previous | Return to table of contents | Next