



























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




























^ You can run the Tcl script using the program “tclsh” as: ~>^ tclsh^ simple.tcl Executing simple^ tcl
script Read^ line:
Writing^ a^ sentence
into^ file
~> ^ Let us observe the syntax:^ -^ Lines beginning with “#” are treated as comments^ -^ The symbol “$” is used to obtain the
-^ The “set” method is used to assign values to variables. Note that the “$”does not appear when something is being assigned to the variable -^ The effect of parentheses in math is obtained by using [] – e.g., [open …]indicates that the code in the brackets is evaluated first and then assignedto f1 -^ “puts”, “gets”, “open” are all Tcl commands. “puts $f1 …” indicates thatwe are pas
^ Simple.tcl thus opens a file called “try”, writes a sentence andreads from it
connected to node 7 with a link of 15Mbps, node 7 to node 3 with 25Mbpsand so on. Write a tcl script to read this file and interpret its contents.Your output should look like: Link 1: Node 1 to Node 7; Bandwidth: 15M Link 2: Node 7 to Node 3; Bandwidth: 25M You might want to use the following functions: # Create an empty list in l1 set l1 [list] # Concatenate a list/string with another and assign to l2 set l2 [concat $l1 $s1] # Access^ an
instproc^
init^ {^ }^
$self^ instvar
nodes^ ns set^ ns^ [Simulator
instance] set^ nodes(1)
[$ns^ node] set^ nodes(2)
[$ns^ node]
^ To understand all aspects in the above example you have toknow the basics of Object oriented programming. I will assumethat you know at least the meaning of these terms: memberfunctions, static functions/variables, instances, constructors ^ The first line is the declaration of “Topology” as a class ^ The function “init” is the equivalent of constructor in C++ - it isthe first function called when an instance of this class is createdby “new” operator. ^ $self is equivalent to the “this” pointer in C++. It refers to thepresent instance within which the function is executing – that isit refers to “itself” ^ “instvar” is used to declare a member variable and similarly“instproc” is used to declare a member function. The syntax of aprocedure is similar to that in Tcl except that the class namehas to come first and the “proc” keyword is replaced byinstproc. The empty braces (“{}”) indicate that the proceduretakes no parameters.