How to suppress a proc's return value in tcl prompt -
i'm relatively new in tcl, in tcl prompt, when invoke proc return value, proc's return value echoed tcl. there way stop (without affecting puts or similar functionality) example
bash$ tclsh % proc {} { puts "hello"; return 34; } % hello 34 %
now how suppress 34 coming screen? appreciated.
update: proc part of tool, earlier did not have return value, conditionally can return value. can called script , there won't problem (as bryan pointed out). , can called interactive prompt, after necessary outputs, return value getting printed unnecessarily. 1) don't have facility of changing user's tclshrc 2) existing scripts should continue work. , seems strange every time proc called, after necessary outputs, number gets printed. user, needless information unless has caught value , wants something. wanted value delivered user, without getting printed prompt/ui (hope i'm clear )
the interactive shell code in tclsh
, wish
print non-empty result. nothing printed, have have last command on “line” produce empty result. command use?
many commands produce empty result:
if 1 {} subst "" format ""
however, shortest probably:
list
thus, write code like:
a;list
of course, becomes useful when command actually produces large result don't want see. in cases, find useful use measures size of result, such as:
set tmp [something_which_produces a_gigantic result]; string length $tmp
the useful commands find string length
, llength
, dict size
.
if absolutely must not print result of command, have write own interactive loop. there 2 ways this, depending on whether running inside event loop or not:
without event loop
this simplistic version checks see if command name in user typed. it's not idea arbitrarily throw away results otherwise!
set accum "" while {[gets stdin line] >= 0} { append accum $line "\n" if {[info complete $accum]} { if {[catch $accum msg]} { puts stderr $msg } elseif {$msg ne "" && ![string match *thespecialcommand* $accum]} { puts $msg } set accum "" } }
with event loop
this handling blocking io case; that's correct thing when input cooked terminal (i.e., default)
fileevent stdin readable handleinput set accum "" proc handleinput {} { global accum if {[gets stdin line] < 0} { exit; # or whatever } append accum $line "\n" if {[info complete $accum]} { if {[catch {uplevel "#0" $accum} msg]} { puts stderr $msg } elseif {$msg ne "" && ![string match *thespecialcommand* $accum]} { puts $msg } set accum "" } } vwait forever; # assuming you're not in wish or have other event loop...
how detect command being executed
the code above uses ![string match *thespecialcommand* $accum]
decide whether throw away command results, ugly. more elegant approach leverages tcl's own built-in hooks use execution trace detect whether command has been called (i'll show non-event-loop version here, brevity). other advantage of is simple extend suppressing output multiple commands: add trace each of them.
trace add execution thespecialcommand enter suppressoutput proc suppressoutput args { # important; not suppress when called inside command if {[info level] == 1} { set ::suppresstheoutput 1 } } # similar here on set accum "" while {[gets stdin line] >= 0} { append accum $line "\n" if {[info complete $accum]} { set suppresstheoutput 0; # <<<<<< note this! if {[catch $accum msg]} { puts stderr $msg } elseif {$msg ne "" && !$suppresstheoutput} { # <<<<<< note this! puts $msg } set accum "" } }
to clear, wouldn't ever in own code! i'd suppress output manually if mattered.
Comments
Post a Comment