Alan Saldanha
Alan Saldanha

Reputation: 87

Equivalent one liner in tcl for an if statement and puts

I have code in tcl for which i want to print only if a variable is enabled. I want to do this in one line if possible.

#in perl 
print "Device name $dev" if(-e $debug);
#in tcl
if {[tvf::svrf debug ]}{
    puts "Device name is $dev"
}

Upvotes: 0

Views: 1581

Answers (5)

syox
syox

Reputation: 3

set dev debug_device
interp alias {} print {} apply {{args {dbg 0}} {expr {$dbg?$args:{}}}}

set debug true
print "Device name is $dev" $debug

A quick one-liner alias with apply as the anonymous function.

Upvotes: 0

Alex P
Alex P

Reputation: 96

Nothing can rightly prevent you from defining this simple Perl command in Tcl:

proc print {msg args} {
  # never mind, this is for fun only
  if {[string match "if(-e *)" $args]} {
    if {[string range $args 6 end-1]} {puts $msg}
  } else {
    # other Perl stuff whatever it was
  }
}

... and use this Perlish print in Tcl:

set dev "'Perl device'"
# ------------
set debug true
print "Device name $dev" if(-e $debug)
print "Is it true?" if(-e true)
print "Is it false?" if(-e false)
# ------------
set debug false
print "2. Device name $dev" if(-e $debug)
print "2. Is it true?" if(-e true)
print "2. Is it false?" if(-e false)

This way you might redefine all of Perl in Tcl ;-)

Upvotes: 0

glenn jackman
glenn jackman

Reputation: 246827

Tcl does not accomodate Perl's statement modifiers. This is entirely due to Tcl's command arg arg ... syntax.

If you like that style, you can create a helper proc:

proc do {script if condition} {
    if {$if ne "if"} {
        error "some usage message"
    }
    if {[uplevel [list expr $condition]]} {
        uplevel $script
    }
}

Then:

% set debug true
true
% do {puts "I'm debugging"} if {$debug}
I'm debugging
% set debug false
false
% do {puts "I'm debugging"} if {$debug}
%

That do proc can be extended with do ... while ... etc.

Upvotes: 0

Donal Fellows
Donal Fellows

Reputation: 137587

I'd split that over multiple lines unless there's going to be many places where you take that decision. If you're doing it in many places, it's better to set up a procedure:

# A single-line way to write this
proc writeDebugMessage args [lindex {{} {puts [lindex $args 0]}} [tvf::svrf debug]]

# At the places where you want to print a message in some cases
writeDebugMessage "Device name is $dev"

That procedure definition is a one-liner way to write this longer version:

if {[tvf::svrf debug]} {
    proc writeDebugMessage args {puts [lindex $args 0]}
} else {
    # This exact form of procedure gets bytecode compiled into nothingness; max efficiency!
    proc writeDebugMessage args {}
}

If this was my real code, I'd use the longer version or even more verbosity!


A true one-liner version is possible, and this is about as short as it gets:

[lindex {list puts} [tvf::svrf debug]] "Device name is $dev"

Not recommended! Defining a helper procedure produces a lot more readable code!

Upvotes: 1

Dinesh
Dinesh

Reputation: 16428

if {[tvf::svrf debug ]} { puts "Device name is $dev" }

Upvotes: 1

Related Questions