TCL Scripting For VLSI Part 3

 Part 5 Procedures
  • A Tcl procedure is a command that is implemented with a Tcl script rather than C code
  • Procedures make it easy to package solutions to problems so that they can be reused easily
  • Procedures also provide a simple way to prototype new features in an application
  • Procedures encapsulate a set of commands and they introduce a local scope for variables
  • A Tcl procedure is defined with the proc command
  • It takes three arguments:
              proc name params body
  • The first argument is the procedure name
  • The second argument is a list of parameter names
  • The last argument is the body of the procedure
Passing parameters by value
  • Tcl procedure once defined can be used like any other Tcl command
  • When it is invoked each argument is assigned to the corresponding parameter and the body is evaluated
  • Eg: proc test { a b} { body }
  • Here a and b are the formal arguments which are going to receive the values passed
  • test 12 (invoking the procedure)
Example:
proc test { a b }{
   puts "$a $b"
}
test 1 2
Output: 1 2
Default  values
  • Procedures can have default parameters so that the caller can leave out some of the command arguments
  • A default parameter is specified with its name and default value proc test { a {b 1} {c 0} } { body }
  • The procedure P2 can be called with one two or three arguments
  • If it is called with only one argument then the parameters b and c take on the values specified in the proc command
Example:
proc test { a {b 5} {c 4}} {
puts $a
puts Sb
puts $c
}
test 1 2 3
test 1 1
test 
Output: 1 2 3
             1 1 4
             1 5 4
Variable number arguments
  • A procedure can take a variable number of arguments by specifying the args keyword as the last parameter
  • When the procedure is called, the args parameter is a list that contains all the remaining values
Output: 75
              70
Recursive Procedures
An example for recursive procedures is shown below:
Output: 5040
              720
Part 6: File Handling
The open command
  • The open command opens a file name in the mode specified and returns a file identifier which is used to perform operations on files
  • The syntax of the open command is
  • open name? Access mode? permissions?
  • Eg: set f1 [open filename]
  • f1 will be holding the file identifier returned by open command which can be used for further operations on the file
The Access modes
  • r opens a file for reading. The file must exist
  • r+ opens a file for reading and writing. The file must exist
  • w opens for writing. Truncate if it exists Create if it does not exist
  • w+ opens for reading and writing .Truncate or create
  • a opens for writing. Data is appended to the file
  • a+ opens for reading and writing. Data is appended
Reading and Writing
  • The standard I/O channels are already open for use
  • The standard input channel is stdin
  • The standard output channel is stdout
  • The standard error channel is stderr
  • Other I/O channels are returned by the open command
Reading a File
Output: vlsi
Writing a File
  • Puts command is used to write to an open file.
Part 7: Regular Expressions
  • The "regexp" command is used to match a regular expression in Tcl.
  • The building block of a regular expression is an atom
  • A simple regular expression consist of one or more atoms
  • The regular expression pattern matches any string containing the pattern
  • The regexp command invokes regular expression matching
  • It takes two arguments in its simplest form, the regular expression pattern and an input string
  • It returns 0 or 1 to indicate whether or not the pattern matched the input string
  • The syntax is regexp pattern string
Example: puts [ regexp vlsi "vlsi4freshers"]
Output: 1
Special characters
  • (.) Matches any single character. Eg: regexp v.s vlsi
  • ^ Matches the beginning of the string : Eg: regexp ^vl vlsi4freshers
  • $ Matches the end of the string : Eg: regexp vls$ companyvlsi
  • [chars] Matches any character from the list of chars
  • (pattern) matches any string containing pattern
Output: Full Match: vlsi4freshers
Sub Match1: vlsi
Sub Match2: freshers
SHARE

vlsi4freshers

Hi I’m Designer of this blog.

    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment