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

rename: delete proc inside itself

19 views
Skip to first unread message

Harald Oehlmann

unread,
Apr 6, 2010, 9:26:36 AM4/6/10
to
I want to run a proc only once.
It came to my mind to do that:
proc ::runonce args {
# some processing
puts $args
# delete myself
rename ::runonce ::
}
Test with TCL8.5.8:
% runonce ABC
ABC
% runonce ABC
invalid command name "runonce"

Is this generally allowed ?

Thank you,
Harald

Andreas Leitgeb

unread,
Apr 6, 2010, 9:36:25 AM4/6/10
to
Harald Oehlmann <wort...@yahoo.de> wrote:
> I want to run a proc only once.
> It came to my mind to do that:
> proc ::runonce args {
> # some processing
> puts $args
> # delete myself
> rename ::runonce ::
> }
> Is this generally allowed ?

It is, but probably not the best idea...

If you cannot control when it gets called, then I'd think it's better to not
just remove it, but rather to replace it with one throwing a specific message.

Just after or instead of the rename:
proc ::runonce args {error "Run me twice: shame on *you*!"}

Andreas Leitgeb

unread,
Apr 6, 2010, 9:42:02 AM4/6/10
to
> Harald Oehlmann <wort...@yahoo.de> wrote:
>> rename ::runonce ::
>> Is this generally allowed ?

I missed the other bit:

Only rename with an empty target name will actually remove the procedure.
Renaming it to "::" will do just that:

% proc foo args {puts hello}
% foo
hello
% rename foo ::
% ::
hello
% rename :: {} ;# now, it's really gone.

Harald Oehlmann

unread,
Apr 6, 2010, 10:17:02 AM4/6/10
to
> Only rename with an empty target name will actually remove the procedure.
Sorry, was a miss-read by me from the wiki.

So lets retry:

proc ::runonce args {
# some processing
puts $args
# delete myself
rename ::runonce ""
}

3 % runonce ABC
ABC
% runonce ABC
% info commands runonce
%

So now, it seams to be in a semi-existing state.
"runonce" does not give any error but info command does not return it.

Don Porter

unread,
Apr 6, 2010, 10:27:39 AM4/6/10
to

If that's an accurate transcript, you have a buggy interp. Double
check, get more details and report.

--
| Don Porter Mathematical and Computational Sciences Division |
| donald...@nist.gov Information Technology Laboratory |
| http://math.nist.gov/~DPorter/ NIST |
|______________________________________________________________________|

Harald Oehlmann

unread,
Apr 6, 2010, 11:10:15 AM4/6/10
to
> > So now, it seams to be in a semi-existing state.
> > "runonce" does not give any error but info command does not return it.
>
> If that's an accurate transcript, you have a buggy interp.  Double
> check, get more details and report.

I think I have hit myself by the interactive interpreter magic
executing external commands
when no internal one found.
There must be a command "runonce" in the path.
A small window is opened and directly closed...
exec runonce
also gives an empty string back.
---
To resume, it is allowed to rename a proc inside itself including
deletion.

Thank you all for answering. I will add a note on the wiki,
Harald

0 new messages