From: Harald Oehlmann on
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
From: Andreas Leitgeb on
Harald Oehlmann <wortkarg2(a)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*!"}

From: Andreas Leitgeb on
> Harald Oehlmann <wortkarg2(a)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.

From: Harald Oehlmann on
> 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.
From: Don Porter on
Harald Oehlmann wrote:
>> 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.

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.porter(a)nist.gov Information Technology Laboratory |
| http://math.nist.gov/~DPorter/ NIST |
|______________________________________________________________________|