Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Stackoverflow question: Hidden Features of Tcl

44 views
Skip to first unread message

Colin Macleod

unread,
Jun 22, 2009, 7:24:16 AM6/22/09
to
I'm sure you can come up with more answers to this:
http://stackoverflow.com/questions/1024711/hidden-features-of-tcl

Colin.

Kevin Kenny

unread,
Jun 22, 2009, 7:42:14 AM6/22/09
to

Good heavens, "hidden or handy features."

Well, you did a good job on enumerating the hidden features; the
one you mentioned is the only intentional Easter Egg of which I'm
aware. (Did you know that I fixed a bug in it when I rewrote [clock]
in 8.5?)

As far as *handy* features go - we've got a Wiki full of 'em.
I'm not even sure where to begin without more information about what
the original poster wants to *do*.

--
73 de ke9tv/2, Kevin

Arjen Markus

unread,
Jun 22, 2009, 8:43:50 AM6/22/09
to

Perhaps one can consider the small assortment of "magical" variables,
such as tcl_precision to be hidden features? (Or rather: the fact that
they are "magical").

Regards,

Arjen

Jonathan Bromley

unread,
Jun 22, 2009, 10:39:07 AM6/22/09
to
On Mon, 22 Jun 2009 04:24:16 -0700 (PDT), Colin Macleod wrote:

>I'm sure you can come up with more answers to this:
>http://stackoverflow.com/questions/1024711/hidden-features-of-tcl
>

So I read the whinges about [expr] and thought for a while,
and then this popped into my head unbidden:

tclsh% proc unknown args {uplevel 1 [linsert $args 0 expr]}

Tcl never ceases to amaze and delight. Now I have a
desk calculator any time I want one, right there on the
tclsh command line. [Yes, I know it's far from perfect...]
--
Jonathan Bromley, Consultant

DOULOS - Developing Design Know-how
VHDL * Verilog * SystemC * e * Perl * Tcl/Tk * Project Services

Doulos Ltd., 22 Market Place, Ringwood, BH24 1AW, UK
jonathan...@MYCOMPANY.com
http://www.MYCOMPANY.com

The contents of this message may contain personal views which
are not the views of Doulos Ltd., unless specifically stated.

colin....@tesco.net

unread,
Jun 22, 2009, 12:35:24 PM6/22/09
to
On 22 June, 15:39, Jonathan Bromley <jonathan.brom...@MYCOMPANY.com>
wrote:

>
> So I read the whinges about [expr] and thought for a while,
> and then this popped into my head unbidden:
>
> tclsh% proc unknown args {uplevel 1 [linsert $args 0 expr]}
>

That's neat - I posted it on SO.

Colin.

Jonathan Bromley

unread,
Jun 22, 2009, 5:10:03 PM6/22/09
to
On Mon, 22 Jun 2009 09:35:24 -0700 (PDT), colin....@tesco.net
wrote:

>> tclsh% proc unknown args {uplevel 1 [linsert $args 0 expr]}
>
>That's neat - I posted it on SO.

The even neater version

proc unknown args { expr $args }

is already on the Wiki, though I think mine's better :-)

It raises another interesting question, though.

Suppose you have some proc ([unknown] in that case, but
could be anything) that you plan to redefine repeatedly.
And suppose you want to keep the existing implementation
as a fallback, so that each redefinition ADDS some new
capability to the proc:

rename stuff _stuff
proc stuff args {
if {it's a special case} {
do my own thing
} else {
# fall back to existing implementation
uplevel 1 [linsert $args _stuff]; # or uplevel 1 _stuff {*}$args
}
}

Now, suppose I want to be able to do that over and
over again so that I can extend "stuff" as often as I wish.
I need a sort of stack of renames. Is there a really neat
way to do this?

Bruce Hartweg

unread,
Jun 22, 2009, 7:06:38 PM6/22/09
to
Jonathan Bromley wrote:
>
> Suppose you have some proc ([unknown] in that case, but
> could be anything) that you plan to redefine repeatedly.
> And suppose you want to keep the existing implementation
> as a fallback, so that each redefinition ADDS some new
> capability to the proc:
>
> rename stuff _stuff
> proc stuff args {
> if {it's a special case} {
> do my own thing
> } else {
> # fall back to existing implementation
> uplevel 1 [linsert $args _stuff]; # or uplevel 1 _stuff {*}$args
> }
> }
>
> Now, suppose I want to be able to do that over and
> over again so that I can extend "stuff" as often as I wish.
> I need a sort of stack of renames. Is there a really neat
> way to do this?

just use [info commands] to see if your renamed stuff exists
and keep changing it (in a repeatable, deterministic way) until
you get to an unused name and then us it as the target of the
rename and fallback call

Bruce

GPS

unread,
Jun 22, 2009, 10:18:19 PM6/22/09
to
Jonathan Bromley wrote:

> On Mon, 22 Jun 2009 04:24:16 -0700 (PDT), Colin Macleod wrote:
>
>>I'm sure you can come up with more answers to this:
>>http://stackoverflow.com/questions/1024711/hidden-features-of-tcl
>>
>
> So I read the whinges about [expr] and thought for a while,
> and then this popped into my head unbidden:
>
> tclsh% proc unknown args {uplevel 1 [linsert $args 0 expr]}
>
> Tcl never ceases to amaze and delight. Now I have a
> desk calculator any time I want one, right there on the
> tclsh command line. [Yes, I know it's far from perfect...]

Here's a related page from the Wiki:
tclsh as a powerful calculator
http://wiki.tcl.tk/11933

-George

Arjen Markus

unread,
Jun 23, 2009, 3:40:07 AM6/23/09
to

While we are on the subject of calculators, why not add
http://wiki.tcl.tk/23034 to the lot?

Okay, it is not simply Tcl, but it sure is a nice little trick.

(And http://wiki.tcl.tk/17308 and kin are another unexpected
use of Tcl/Tk ...)

Regards,

Arjen

dkf

unread,
Jun 23, 2009, 4:32:59 AM6/23/09
to
On 22 June, 23:10, Jonathan Bromley <jonathan.brom...@MYCOMPANY.com>
wrote:

> Now, suppose I want to be able to do that over and
> over again so that I can extend "stuff" as often as I wish.
> I need a sort of stack of renames.  Is there a really neat
> way to do this?

I'd suggest starting with http://wiki.tcl.tk/2776 which focusses on
[unknown]. The other way (renaming and chaining) can work, but be
careful not to rename from one namespace into another; procedures have
the misfeature that they don't remember what namespace they were
defined in. (Yes, this has annoyed me a lot. I'm told that it can't be
fixed without breaking code...)

Donal.

0 new messages