Showing posts with label tclsh. Show all posts
Showing posts with label tclsh. Show all posts

16 September, 2008

Tcl Package Example

I often struggle setting up a Tcl script that utilizes packages. For convenience I'm publishing a template for scripts that capitalize on packages.


::::::::::::::
process
::::::::::::::
#!/usr/bin/tclsh


proc log { msg } {
set lvl [expr [info level] -2]
set indent [string repeat " " $lvl]
tclLog "$indent\___$msg"
}

proc traceLog { } {
set uplevel [expr [info level] -1]
set callSig [info level $uplevel]
set el [split $callSig]
log "call trace: [namespace current][lindex $el 0]([lrange $el 1 end])"

}

proc packageInit { } {
upvar #0 auto_path auto_path

log "initializing system"
set pathName [file dirname [info script]]
log "discovered path name '$pathName'"
pkg_mkIndex $pathName tcl/*.tcl
lappend auto_path $pathName
log "done initializing system"
}

proc proc01 { a b c d } {
traceLog
}

proc proc02 { } {
traceLog
proc01 2 4 6 8
}

#-----main-----
packageInit
package require package01
package01::function01

log "main thread initializing"
proc01 1 2 3 4
proc02
log "main thread terminating"


::::::::::::::
tcl/package01.tcl
::::::::::::::
package provide package01 1.0

namespace eval package01 {
proc function01 { } {
puts "...function01() method entry"
puts "...function01() method exit"
}
}

07 May, 2008

Parsing File Lists with Tcl

Hey all,
Been a bit of time since my last post. I've been struggling attaining some new information to post, but haven't found anything 'post worthy'. Instead, I wandered back in the ol' memory to find a useful nugget of info that you may find useful.

I love Tcl. From the bottom of my heart, this is the scripting language of choice for this cubicle dweller. A routine use for scripting languages is to batch process vast lists of files. Any well behaved *nix user probably uses camel back file naming notations (e.g. SomeFileForYou.txt), while others may use a underscore delimited notation (e.g. some_file_for_you.txt), but Windows enthusiasts (many of my teammates included) insist on using spaces in their file names (e.g. Some File For You.txt).

Batch processing filenames with spaces can present a problem if you don't parse the list properly. Since Tcl lists are generally seperated by spaces, file names with spaces are parsed as individual files for each word.

Observe:

$ ls /var/tmp/someDir
total 8
-rw-r--r-- 1 fatslowkid fatslowkid 6 2008-05-07 22:38 Some Longer File Name.txt
-rw-r--r-- 1 fatslowkid fatslowkid 7 2008-05-07 22:38 Some Long File Name.txt

$ cat listFiles
1 #!/usr/bin/tclsh
2
3 set fileList [exec find /var/tmp/someDir/]
4 #set fileList [split $fileList "\n"]
5
6 foreach file $fileList {
7 puts '$file'
8 }


Running will result in:
$ ./listFiles
'/var/tmp/someDir/'
'/var/tmp/someDir/Some'
'Long'
'File'
'Name.txt'
'/var/tmp/someDir/Some'
'Longer'
'File'
'Name.txt'


Uncommenting line 4 which splits the list using the newline character as the delimiter solves the problem. Observe:

$ ./listFiles
'/var/tmp/someDir/'
'/var/tmp/someDir/Some Long File Name.txt'
'/var/tmp/someDir/Some Longer File Name.txt'


TaDa. Hope you find this useful, I certainly have over the years.