Pages

Wednesday, October 21, 2009

TCL package

Q. Write a TCL program that meet he following requirements
Create a package “hw_config” which contains 3 procedures namely “Lockport”, “Freeport”, and “Breakport”. There must be an array “socket” global to the package “hw_config” which is defined with the default parameters. The parameters defined in array “socket” is used by all the 3 procedures (Lockport, Freeport, Breakport), whenever required. If a user wants to override the default params it will be passed to the procedure as an array. Within the procedure we have to use the user passed params (if not found, use default)

Solution:

File name 1: sample.tcl
Content:
package require hw_config 1.0

#Setting port lock parameters
set socket(ip) "10.100.11.153"
::hw_config::lockport socket

File name 2: hw_config.tcl ; # used for package
Content:
#!/usr/bin/expect
package require Expect
package provide hw_config 1.0

namespace eval hw_config {
variable socket
array set socket {ip 10.10.10.10 port 7}
namespace export lockport
namespace export freeport
namespace export breakport
}

proc ::hw_config::lockport { soc } {
upvar $soc sock
variable socket

#overriding the default values with the values supplied

foreach var [array names sock] {
set socket($var) $sock($var)
}

puts " Specified port $socket(ip):$socket(port) locked"
}

proc ::hw_config::freeport { } {
puts " Specified ports are freed"
}
proc ::hw_config::breakport { } {
puts " Specified ports are brocken"
}