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

Testing the existence of a procedure

43 views
Skip to first unread message

Roderick

unread,
Sep 28, 2018, 3:09:52 PM9/28/18
to

Is there a better way than than the following:

if {[llength [info procs db]]} {db close}

???

Robert Heller

unread,
Sep 28, 2018, 3:37:57 PM9/28/18
to
At Fri, 28 Sep 2018 19:03:11 +0000 Roderick <hru...@gmail.com> wrote:

>
>
> Is there a better way than than the following:
>
> if {[llength [info procs db]]} {db close}

catch {db close}

OR if there might be an error from the proc that you want to pass up the
stack:

if {[catch {db close} result]} {
if {[regexp {^invalid command name } $result] < 1} {
error $result
}
}



>
> ???
>

--
Robert Heller -- 978-544-6933
Deepwoods Software -- Custom Software Services
http://www.deepsoft.com/ -- Linux Administration Services
hel...@deepsoft.com -- Webhosting Services

Don Porter

unread,
Sep 28, 2018, 3:41:14 PM9/28/18
to
On 09/28/2018 03:03 PM, Roderick wrote:
> Is there a better way than than the following:
>
> if {[llength [info procs db]]} {db close}

if {[namespace which db] ne {}} {...}

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

Roderick

unread,
Sep 29, 2018, 8:01:54 AM9/29/18
to

On Fri, 28 Sep 2018, Robert Heller wrote:

> catch {db close}
>
> OR if there might be an error from the proc that you want to pass up the
> stack:
>
> if {[catch {db close} result]} {
> if {[regexp {^invalid command name } $result] < 1} {
> error $result
> }
> }

Well, I wanted to pass any error of a real, existent db command,
as in my original script. Catch and some tests with regexp does it.
It corresponds straightforward to my original intention, although I find
the command "catch" in some way brutal force. :)

I suspect all alternatives are equivalent.

Thanks
Rodrigo

Donal K. Fellows

unread,
Sep 29, 2018, 10:17:08 AM9/29/18
to
On 28/09/2018 20:37, Robert Heller wrote:
> OR if there might be an error from the proc that you want to pass up the
> stack:
>
> if {[catch {db close} result]} {
> if {[regexp {^invalid command name } $result] < 1} {
> error $result
> }
> }

With 8.6's [try] (and the work we put in to make error codes more
consistently useful), you can do this instead:

try {
db close
} trap {TCL LOOKUP COMMAND} {} {
# Ignore it or log it or something...
}

The good thing is that this won't trap anything else.

Donal.
--
Donal Fellows — Tcl user, Tcl maintainer, TIP editor.

Dave

unread,
Sep 29, 2018, 1:40:34 PM9/29/18
to
On 9/29/2018 9:17 AM, Donal K. Fellows wrote:
> trap {TCL LOOKUP COMMAND}

Since "try" was not available when I learned Tcl I don't think I've ever
attempted to use it. This post made me curious so I tried it on win7,
Tcl 8.6.8:

% catch { jkjkjk close } m n
% set n
-code 1 -level 0 -errorstack {INNER {invokeStk1 jkjkjk close}}
-errorcode NONE -errorinfo {invalid command name "jkjkjk"
while executing
"jkjkjk close "} -errorline 1

How would "trap {TCL LOOKUP COMMAND}" match?

Here's the same using "try":

% try { kkkkk close } trap {TCL LOOKUP COMMAND} {} { puts hit }
invalid command name "kkkkk"

What am I missing here?


--
computerjock AT mail DOT com

Roderick

unread,
Sep 29, 2018, 3:17:59 PM9/29/18
to


On Sat, 29 Sep 2018, Donal K. Fellows wrote:

> With 8.6's [try] (and the work we put in to make error codes more
> consistently useful), you can do this instead:
>
> try {
> db close
> } trap {TCL LOOKUP COMMAND} {} {
> # Ignore it or log it or something...
> }
>
> The good thing is that this won't trap anything else.

Well, and how do I know these error codes?

In the tcls man page for "open" I see: "ERROR CODES (Windows only)".
Not interesting for me.

And since there is no man page for non existent procedures, I have
no idea where to find the error codes for them. Well:

--
% xyz
invalid command name "xyz"
% puts $errorCode
TCL LOOKUP COMMAND xyz
--

I suppose, it analogue for every non existent function????

Rodrigo
0 new messages