Another Ruby-inspired Tcl construct
Inspired by the Ruby construct where you can embed something like
#{"<em>some string</em>" if <em>condition</em>}
inside a string, I have started using this syntax in Tcl from time to time:
DELETE FROM gcms_rels WHERE revision_id = :revision_id [if { [exists_and_not_null rel_options] } { set x "AND related_item_id IN ([join $rel_options ", "])" }]
It is pretty much exactly the same thing as in Ruby, and also very similar to the use of
ad_decode
that we’ve been using in OpenACS for a while. But it is superior to ad_decode
because it is more readable, and because you can have elseif
, switch
, or any other construct in there.
The only ugly thing is the dummy set x
part, which is required because, unlike Ruby, Tcl will interpret "foo"
on a line by itself as the name of a command to be executed. I also tried using return
, but that blows out of the entire proc, so isn’t usable.
If you have a cleaner solution, I would like to hear about it.
About Calvin Correli
I've spent the last 17 years learning, growing, healing, and discovering who I truly am, so that I'm now living every day aligned with my life's purpose.
2 comments
Leave a comment