• why is this variable not in global namespace?

    From Michael Soyka@21:1/5 to All on Thu Mar 16 20:33:24 2023
    As I was investigating a "package require" issue, I came across the
    following surprising, to me, behavior. Needless to say, I don't
    understand it.

    I expected the following code to instantiate the variable "myGlobal" in
    the global namespace:

    namespace eval Wrapper {
    global myGlobal
    if { ![info exists myGlobal] } {
    set myGlobal [namespace current]
    }
    puts "myGlobal: $myGlobal"
    foreach varName {myGlobal ::myGlobal ::Wrapper::myGlobal} {
    puts "Exists? $varName: [info exists $varName]"
    }
    }

    but the output:

    myGlobal: ::Wrapper
    Exists? myGlobal: 1
    Exists? ::myGlobal: 0
    Exists? ::Wrapper::myGlobal: 1

    says that "myGlobal" instead lives in the "Wrapper" namespace.

    If the above code is preceded by this statement:

    set myGlobal XXX

    we get this output:

    myGlobal: XXX
    Exists? myGlobal: 1
    Exists? ::myGlobal: 1
    Exists? ::Wrapper::myGlobal: 0

    which shows "myGlobal" is defined in the global namespace.

    Finally, if we change the namespace command in the first example to a proc:

    proc Wrapper {} {
    global myGlobal
    set myGlobal XXX
    }
    Wrapper
    puts "Exists? myGlobal [info exists myGlobal]"

    "myGlobal" is defined in the global namespace which is expected.

    I get that in examples 1 and 3 "myGlobal" does not exist initially but
    the "namespace" and "proc" commands yield different results. So, the
    question is why the difference?

    TIA for the coming education!

    -mike

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From dave bruchie@21:1/5 to All on Fri Mar 17 07:44:03 2023
    the first line of the global man page says: This command has no effect unless executed in the context of a proc body.

    If you remove the "global myGlobal: line from your example, there should be no change in the results.
    The variable is created (or not) in the normal way by the following set command.

    Dave B

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Michael Soyka@21:1/5 to dave bruchie on Fri Mar 17 11:32:42 2023
    On 03/17/2023 10:44 AM, dave bruchie wrote:
    the first line of the global man page says: This command has no effect unless executed in the context of a proc body.

    If you remove the "global myGlobal: line from your example, there should be no change in the results.
    The variable is created (or not) in the normal way by the following set command.

    Dave B
    Hi Dave,

    Thanks for the reply. I had seen the clear statement in that manpage
    but I guess it didn't register.

    So, it looks like you don't need to use a "variable" command in a
    namespace in order to create that variable in the namespace. The reason
    for using one, outside of a proc, is to ensure the variable is created
    in the namespace just in case that variable also exists in the global namespace.

    The method used to resolve variable names is explained in the "Name
    Resolution" section of the "namespace" manpage.

    -mike

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Paul Walton@21:1/5 to Michael Soyka on Fri Mar 17 14:55:21 2023
    On Friday, March 17, 2023 at 11:32:48 AM UTC-4, Michael Soyka wrote:
    just in case that variable also exists in the global

    I think globals become much more manageable and readable if you just enforce a style of always referring to them with the fully qualified name, such as ::VAR and also use all caps. The global command is unnecessary and hinders readability in my opinion.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)