@node Tcl, Tcl/Tk, SyslogScan/WhereIs, Module List
@unnumbered Tcl extension module for Perl


@unnumberedsec NAME

Tcl - Tcl extension module for Perl

@unnumberedsec DESCRIPTION

The Tcl extension module gives access to the Tcl library with
functionality and interface similar to the C functions of Tcl.
In other words, you can

@table @asis
@item create Tcl interpreters
The Tcl interpreters so created are Perl objects whose destructors
delete the interpreters cleanly when appropriate.

@item execute Tcl code in an interpreter
The code can come from strings, files or Perl filehandles.

@item bind in new Tcl procedures
The new procedures can be either C code (with addresses presumably
obtained using @emph{dl_open} and @emph{dl_find_symbol}) or Perl subroutines
(by name, reference or as anonymous subs). The (optional) deleteProc
callback in the latter case is another perl subroutine which is called
when the command is explicitly deleted by name or else when the
destructor for the interpreter object is explicitly or implicitly called.

@item Manipulate the result field of a Tcl interpreter
@itemx Set and get values of variables in a Tcl interpreter
@itemx Tie perl variables to variables in a Tcl interpreter
The variables can be either scalars or hashes.

@end table
@unnumberedsubsec Methods in class Tcl

To create a new Tcl interpreter, use

@example
$i = new Tcl;
@end example

The following methods and routines can then be used on the Perl object
returned (the object argument omitted in each case).

@table @asis
@item Init ()
Invoke @emph{Tcl_Init} on the interpeter.

@item Eval (STRING)
Evaluate script STRING in the interpreter. If the script returns
successfully (TCL_OK) then the Perl return value corresponds to
interp->result otherwise a die exception is raised with the $@@
variable corresponding to interp->result. In each case, @emph{corresponds}
means that if the method is called in scalar context then the string
interp->result is returned but if the method is called in list context
then interp->result is split as a Tcl list and returned as a Perl list.

@item GlobalEval (STRING)
Evalulate script STRING at global level. Otherwise, the same as
Eval() above.

@item EvalFile (FILENAME)
Evaluate the contents of the file with name FILENAME. Otherwise, the
same as Eval() above.

@item EvalFileHandle (FILEHANDLE)
Evaluate the contents of the Perl filehandle FILEHANDLE. Otherwise, the
same as Eval() above. Useful when using the filehandle DATA to tack
on a Tcl script following an __END__ token.

@item call (PROC, ARG, ...)
Looks up procedure PROC in the interpreter and invokes it directly with
arguments (ARG, ...) without passing through the Tcl parser. For example,
spaces embedded in any ARG will not cause it to be split into two Tcl
arguments before being passed to PROC.

@item result ()
Returns the current interp->result field. List v. scalar context is
handled as in Eval() above.

@item CreateCommand (CMDNAME, CMDPROC, CLIENTDATA, DELETEPROC)
Binds a new procedure named CMDNAME into the interpreter. The
CLIENTDATA and DELETEPROC arguments are optional. There are two cases:

(1) CMDPROC is the address of a C function

(presumably obtained using @emph{dl_open} and @emph{dl_find_symbol}. In this case
CLIENTDATA and DELETEPROC are taken to be raw data of the ClientData and
deleteProc field presumably obtained in a similar way.

(2) CMDPROC is a Perl subroutine

(either a sub name, a sub reference or an anonymous sub). In this case
CLIENTDATA can be any perl scalar (e.g. a ref to some other data) and
DELETEPROC must be a perl sub too. When CMDNAME is invoked in the Tcl
interpeter, the arguments passed to the Perl sub CMDPROC are

@example
(CLIENTDATA, INTERP, LIST)
@end example

where INTERP is a Perl object for the Tcl interpreter which called out
and LIST is a Perl list of the arguments CMDNAME was called with.
As usual in Tcl, the first element of the list is CMDNAME itself.
When CMDNAME is deleted from the interpreter (either explicitly with
DeleteCommand or because the destructor for the interpeter object
is called), it is passed the single argument CLIENTDATA.

@item DeleteCommand (CMDNAME)
Deletes command CMDNAME from the interpreter. If the command was created
with a DELETEPROC (see CreateCommand above), then it is invoked at
this point. When a Tcl interpreter object is destroyed either explicitly
or implicitly, an implicit DeleteCommand happens on all its currently
registered commands.

@item SetResult (STRING)
Sets interp->result to STRING.

@item AppendResult (LIST)
Appends each element of LIST to interp->result.

@item AppendElement (STRING)
Appends STRING to interp->result as an extra Tcl list element.

@item ResetResult ()
Resets interp->result.

@item SplitList (STRING)
Splits STRING as a Tcl list. Returns a Perl list or the empty list if
there was an error (i.e. STRING was not a properly formed Tcl list).
In the latter case, the error message is left in interp->result.

@item SetVar (VARNAME, VALUE, FLAGS)
The FLAGS field is optional. Sets Tcl variable VARNAME in the
interpreter to VALUE. The FLAGS argument is the usual Tcl one and
can be a bitwise OR of the constants $Tcl::GLOBAL_ONLY,
$Tcl::LEAVE_ERR_MSG, $Tcl::APPEND_VALUE, $Tcl::LIST_ELEMENT.

@item SetVar2 (VARNAME1, VARNAME2, VALUE, FLAGS)
Sets the element VARNAME1(VARNAME2) of a Tcl array to VALUE. The optional
argument FLAGS behaves as in SetVar above.

@item GetVar (VARNAME, FLAGS)
Returns the value of Tcl variable VARNAME. The optional argument FLAGS
behaves as in SetVar above.

@item GetVar2 (VARNAME1, VARNAME2, FLAGS)
Returns the value of the element VARNAME1(VARNAME2) of a Tcl array.
The optional argument FLAGS behaves as in SetVar above.

@item UnsetVar (VARNAME, FLAGS)
Unsets Tcl variable VARNAME. The optional argument FLAGS
behaves as in SetVar above.

@item UnsetVar2 (VARNAME1, VARNAME2, FLAGS)
Unsets the element VARNAME1(VARNAME2) of a Tcl array.
The optional argument FLAGS behaves as in SetVar above.

@end table
@unnumberedsubsec Linking Perl and Tcl variables

You can tie a Perl variable (scalar or hash) into class Tcl::Var
so that changes to a Tcl variable automatically "change" the value
of the Perl variable. In fact, as usual with Perl tied variables,
its current value is just fetched from the Tcl variable when needed
and setting the Perl variable triggers the setting of the Tcl variable.

To tie a Perl scalar @emph{$scalar} to the Tcl variable @emph{tclscalar} in
interpreter @emph{$interp} with optional flags @emph{$flags} (see SetVar
above), use

@example
tie $scalar, Tcl::Var, $interp, "tclscalar", $flags;
@end example

Omit the @emph{$flags} argument if not wanted.

To tie a Perl hash @emph{%hash} to the Tcl array variable @emph{array} in
interpreter @emph{$interp} with optional flags @emph{$flags}
(see SetVar above), use

@example
tie %hash, Tcl::Var, $interp, "array", $flags;
@end example

Omit the @emph{$flags} argument if not wanted. Any alteration to Perl
variable @emph{$hash@{"key"@}} affects the Tcl variable @emph{array(key)}
and @emph{vice versa}.

@unnumberedsubsec AUTHOR

Malcolm Beattie, mbeattie@@sable.ox.ac.uk, 23 Oct 1994.

