J-Sim Official

A List of Frequently Used Tcl/Jacl Commands

"Scripting languages such as Perl and Tcl represent a very different style of programming than system programming languages such as C or JavaTM. Scripting languages are designed for ``gluing'' applications; they use typeless approaches to achieve a higher level of programming and more rapid application development than system programming languages."

--John Ousterhout, IEEE Computer, March 1998

In this document, we give a list of Tcl/Jacl commands that are frequently used in J-Sim.  We do not intend to cover all the Tcl/Jacl commands. Instead we only cover those frequently used Tcl/Jacl commands in J-Sim, so that users can get a quick start. For a complete description of Tcl/Jacl commands, refer to the Tcl and Jacl websites.


List of Frequently-Used Tcl Commands in J-Sim
close, expr, for, foreachif, lindex, list, open, proc, puts, set, while
 
List of Frequently-Used Jacl Commands
java::new | java::call | java::cast | java::field | java::null

1. Frequently-Used Tcl Commands in J-Sim

Reference
http://dev.scriptics.com/man/tcl8.4/TclCmd/contents.htm
List of Frequently-Used Tcl Commands in J-Sim
close, expr, for, foreachif, lindex, list, open, proc, puts, set, while

Descriptions

close
Close an open channel

expr
Concatenate arg's (adding separator spaces between them), evaluate the result as a Tcl expression, and return the value.
  • Synopsis:
    expr arg ?arg arg ...?
  • Operands
    • numerical value, integer or float point
    • Tcl variables $b
    • String enclosed in double quotes such as "How are You?"
    • Tcl command enclosed in braces [expr $a + $b]
    • Maths function, e.g. sin($x)
  • Operators:
    • Unary minus, unary plus, bit-wise NOT, logical NOT - + ~ !
    • Addition, subtraction, multiply, divide, remainder +-* / % 
    • Bit shift << >> 
    • Logical comparison < > <= >= == != 
    • Bit-wise AND, exclusive OR, OR & ^ | 
    • Logcal AND, OR && || 
    • x?:y:z 
  • Examples: Suppose the variable a has the value 3 and the variable b has the value 6.
    expr 3.1 + $a    6.1
    expr 2 + "$a.$b"    5.6
    expr {{word one} < "word $a"}    0
  • Reference: http://dev.scriptics.com/man/tcl8.4/TclCmd/expr.htm

for
``For'' loop; for is a looping command, similar in structure to the C for statement. The start, next, and body arguments must be Tcl command strings, and test is an expression string.

foreach
Implement a loop where the loop variable(s) take on values from one or more lists.

if
Execute scripts conditionally

list
Create a list; return a list comprised of all the args, or an empty string if no args are specified.

lindex
Retrieve an element from a list. It treats list as a Tcl list and returns the index'th element from it (0 refers to the first element of the list).

open
Open a file-based or command pipeline channel
  • Synopsis:
    open fileName
    open fileName access
    open fileName access permissions
  • Access
    r    Open the file for reading only; the file must already exist. This is the default value if 
          access
    is not specified.
    r+  Open the file for both reading and writing; the file must already exist.
    w   Open the file for writing only. Truncate it if it exists. If it doesn't exist, create a new file.
    w+ Open the file for reading and writing. Truncate it if it exists. If it doesn't exist, create a
           new file.
    a    Open the file for writing only. If the file doesn't exist, create a new empty file. Set the
           initial access position to the end of the file.
    a+  Open the file for reading and writing. If the file doesn't exist, create a new empty file. 
          Set the initial access position to the end of the file.
  • Permission: RDONLY, WRONLY or RDWR etc.
  • Examples
    set f [open "HelloWord.html" w]
  • Reference: http://dev.scriptics.com/man/tcl8.4/TclCmd/open.htm

proc
Create a Tcl procedure. It creates a new Tcl procedure named name, replacing any existing command or procedure there may have been by that name.

puts
Writes the characters given by string to the channel given by channelId; the default of channelId is stdout
  • Synopsis:
    puts ?-nonewline? ?channelId? string
  • Option: 
    -nonewline
    Puts normally outputs a newline character after string, but this feature may be suppressed by specifying the -nonewline switch.
  • Examples: puts f "Hello Word"
  • Reference: http://dev.scriptics.com/man/tcl8.4/TclCmd/puts.htm

set
Read and write variables; returns the value of variable varName.

while
Execute the script in body repeatedly as long as the condition specified in test is met

2. Frequently-Used Jacl Commands in J-Sim

Jacl stands for Java Command Language and is a Java implementation of Tcl 8.x. With the advent of Tcl/Java API, we can write extensions for Tcl, in Java code, that will be cross platform. In addition, we can use a new set of Tcl instructions to create, call and manipulate Java objects directly from Tcl.  Using this interface provides an easy way to get access to Java's powerful libraries without having to leave Tcl. In particular, in J-Sim we embed the powerful Tcl scripting language into an existing Java application. 

Reference
http://tcljava.sourceforge.net/docs/website/manual.html
 
List of Frequently-Used Jacl Commands
java::new | java::call | java::cast | java::field | java::null

Descriptions

java::new
Create new instances of Java Objects from Tcl
  • Synopsis
    java::new signature ?arg arg ...?
    The signature argument specifies which class constructor to use for creating the object. Additional parameters to the java::new command are converted to Java objects or primitive values and passed to the constructor.
  • Example
    • set a [java::new String "Hello, world!"]
    • set matrix [java::new int[][] 2 2 {{1 0} {0 1}}
  • Reference
    http://tcljava.sourceforge.net/docs/TclJava/JavaNewCmd.html

java::call
Invoke public static methods from Tcl
  • Synopsis
    java::call ?-noconvert? class signature ?arg arg ...?
    The signature argument specifies which class method to invoke. Additional parameters to the java::call command are converted to Java objects or primitive types and passed to the method.
  • Example
    #return packet header
    set hd [java::call drcl.inet.InetPktHeader create]
  • Reference
    http://tcljava.sourceforge.net/docs/TclJava/JavaCallCmd.html

java::field
Manipulate public fields from Tcl. 
  • Synopsis
    java::field ?-noconvert? objOrClass  fieldSignature ?value fieldSignature value ...?
    The objOrClass argument specifies either a fully qualified name of the declaring class of the field to access, or an object handle. The fieldSignature argument specifies which field to manipulate. If an additional value parameter exists, then the field will be set to value, otherwise the current value of the field is returned.
  • Example
    #set the destination and router_alert in packet header to 10 and true respectively
    $hd setLong [java::field drcl.inet.InetPktHeader DESTINATION] 10
    $hd setBoolean [java::field drcl.inet.InetPktHeader ROUTER_ALERT] true
  • Reference
    http://tcljava.sourceforge.net/docs/TclJava/JavaFieldCmd.html

java::cast
Convert a Java object from one type to another.
  • Synopsis
    java::cast signature javaObj
    The signature argument provides the name of the Java class that the Java object should be cast to and the javaObj provides the Java object that will be cast. Casting is done in a type safe way as defined by the Java Language Specification.
  • Example
    # Get an instance of a String object.
    set string [java::new String "Hi there"]
    # Cast the String to an Object reference
    set object [java::cast Object $string]
    # Cast the Object reference back up to a String reference
    set string2 [java::cast String $object]
  • Reference
    http://tcljava.sourceforge.net/docs/TclJava/JavaCastCmd.html


java::null
Return an object handle that represents the "null" value in Java

* JAVA and all JAVA-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries.

~ END ~