Code Comments
Programming Forum and web based access to our favorite programming groups.I was using the tcllib::dns package and
couldn't get it to load. I traced the
problem down to this small example which
results in the error message
"Couldn't save command result in variable"
set msg(1) one ;# Turn msg into an array
namespace eval ::foo {
catch {package require notFound} msg
}
It appears the global msg and the ::foo msg
variables are getting
.
It's easy to work around the bug--just fully
qualify the second msg--but this appears in
tcllib code.
Keith
Post Follow-up to this messagekeithv wrote:
> I was using the tcllib::dns package and
> couldn't get it to load. I traced the
> problem down to this small example which
> results in the error message
> "Couldn't save command result in variable"
>
> set msg(1) one ;# Turn msg into an array
> namespace eval ::foo {
> catch {package require notFound} msg
> }
>
> It appears the global msg and the ::foo msg
> variables are getting
.
>
> It's easy to work around the bug--just fully
> qualify the second msg--but this appears in
> tcllib code.
The really safe solution is to add the line
variable msg
right before the 'catch ...', within the 'namespace eval ::foo' block.
Sadly, that is the way Tcl's variable resolution is specified: if a
variable is not found in the current namespace, it is looked up as a
global variable. If a global is found, it is used. Otherwise a variable
in the current namespace is created. Nasty, IMO. We will not be able to
fix this in the short run :(
So, the rule is: if you want to make sure that you are using a variable
in the current namespace, do "declare" it with [variable].
Miguel
Post Follow-up to this messagekeithv wrote:
> I was using the tcllib::dns package and
> couldn't get it to load. I traced the
> problem down to this small example which
> results in the error message
> "Couldn't save command result in variable"
>
> set msg(1) one ;# Turn msg into an array
> namespace eval ::foo {
> catch {package require notFound} msg
> }
If you want namespace restricted resolution of a variable name,
use [variable] to set it up:
set msg(1) one ;# Turn msg into an array
namespace eval ::foo {
variable msg
catch {package require notFound} msg ;# Now this means ::foo::msg
}
Tcl prefers to resolve a variable name to any existing variable rather
than create a new one.
--
| Don Porter Mathematical and Computational Sciences Division |
| donald.porter@nist.gov Information Technology Laboratory |
| http://math.nist.gov/~DPorter/ NIST |
|_______________________________________
_______________________________|
Post Follow-up to this message"keithv" <kvetter@gmail.com>,
In a message on 27 Apr 2005 12:37:32 -0700, wrote :
"> I was using the tcllib::dns package and
"> couldn't get it to load. I traced the
"> problem down to this small example which
"> results in the error message
"> "Couldn't save command result in variable"
">
"> set msg(1) one ;# Turn msg into an array
"> namespace eval ::foo {
"> catch {package require notFound} msg
"> }
">
"> It appears the global msg and the ::foo msg
"> variables are getting
.
Nope. You did not declare the msg in the namespace to be a variable.
It is still the global msg and you are trying to set a scalar value in a
variable that was bound to an array.
">
"> It's easy to work around the bug--just fully
"> qualify the second msg--but this appears in
"> tcllib code.
Might be a bug in the tcllib or maybe something else.
">
"> Keith
">
">
\/
Robert Heller ||InterNet: heller@cs.umass.edu
http://vis-www.cs.umass.edu/~heller || heller@deepsoft.com
http://www.deepsoft.com /\FidoNet: 1:321/153
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.