TOC PREV NEXT INDEX

 


Example 1: Cloning Procedures


Scripts that use the bodies of procedures in computations will not work properly if the procedures have been compiled. For example, the script below uses info body to extract the body of one procedure and use it to create another procedure that is identical.


#clone.tcl--
proc len {a} {
    return [string length $a]
}
proc len1 {a} [info body len]
puts "[len {abc}] + [len1 {monkey}]"

The two calls to proc create two procedures, len and len1, with identical bodies.

If you run the clone.tcl file, you get this output:


C:> protclsh83 clone.tcl
3 + 6

Bytecode files, however, do not contain any sources for compiled procedure bodies, and info body returns a standard value.

If you run the clone.tbc file, you get this output:


C:> protclsh83 clone.tbc
called a copy of a compiled script
while executing
"error "called a copy of a compiled script""
(procedure "len1" line 2)
invoked from within
"# Compiled -- no source code available
error "called a copy of a compiled script""
invoked from within
"tbcload::bceval {
TclPro ByteCode 1 0 1.3 8.2
6 0 49 12 0 0 28 0 6 6 6 -1 -1
49
/QE<!(H&s!/HW<!'E'<!*Ki<!/'vpv1fAs!+EE<!2o8X!0fA9v4u8X!1'8X!z
6=t-Ow+..."
(file "clone.tbc" line 17)

Note that the call to len1 resulted in an error being thrown; this error comes from the script returned by the info body len command. The script throws the error rather than failing silently to help you to detect unsupported uses of info body command. If you need to use the body of a procedure in a computation, do not compile that procedure.




http://www.ajubasolutions.com
Voice: (650) 210-0100
Fax: (650) 210-0101
support@ajubasolutions.com
TOC PREV NEXT INDEX