From: Schelte B. <tc...@tc...> - 2024-07-14 14:08:25
|
Ashok, I came up with this small proc: proc nseval {ns body} { namespace eval $ns {} apply [list {} $body $ns] } Then using nseval instead of namespace eval does make the global and variable commands work to provide access to global variables. (I haven't thought too deeply about whether this approach has other problems.) I have created a ticket for the info vars issue: https://core.tcl-lang.org/tcl/tktview/0e4b7fce57 Schelte. On 14/07/2024 14:40, apn...@ya... wrote: > Schelte, > > Thanks for the report. I'll take a look at the messages as you reported but > I was also not aware of the Tcl behaviours you outlined in those last two > paras. Perhaps someone else might comment but looks as though there is no > alternative to explicitly qualifying with :: and the "info vars" issue might > just be that that command has not been adapted to reflect the change in var > resolution in Tcl 9. Probably worth logging a ticket just so it does not get > lost either way. > > /Ashok > > -----Original Message----- > From: Schelte Bron <tc...@tc...> > Sent: Saturday, July 13, 2024 8:59 PM > To: tcl...@li... > Subject: Re: [TCLCORE] Migration tool for Tcl 9 > > Hello Ashok, > > This is a very useful tool. Thanks for creating it. Not only does it > help to quickly find the points that need addressing when wanting to > make an application compatible with Tcl 9.0, it also instills confidence > to know that at least the majority of the potential pitfalls have been > identified. > > As you asked for feedback, here's one situation I came across: When I > run the migration tool on my largest application, I mainly get > [UNDECLARED] errors. The detailed information about this error mentions > writes within a namespace block (outside procs). In my case they are > actually all reads within a namespace block. But anyway, the advise is > "to declare variables explicitly using either `global` or `variable` > depending on whether the intent was to modify a global or namespace > variable." > > I made a small test script that is similar to my use case: > > set dir [pwd] > namespace eval bla { > variable datadir [file join $dir data] > } > > Running the migration tool on this script reports the [UNDECLARED] > error, as expected. So I changed the script according to the recommendation: > > set dir [pwd] > namespace eval bla { > global dir > variable datadir [file join $dir data] > } > > That satisfies the migration tool. However, running the script with tcl > 9.0 throws an error: can't read "dir": no such variable > > That makes sense, because the very first line of the manual page for the > "global" command states: "This command has no effect unless executed in > the context of a proc body". So the proposed solution is ineffective and > the tool should not be fooled by it. > > In fact, what is the suggested way to use global variables in a > namespace block if you don't want to add :: to each reference? In > addition to the `global` command, you can use `variable ::dir` inside a > proc body. This also creates a link from the global variable to a local > variable. Unfortunately, this is equally ineffective in a namespace > block. Is there really no better way than: `namespace upvar :: dir dir`? > > Another confusing thing is that running `info vars` from within the > namespace block returns (among others) the dir variable. That would > suggest that the dir variable is "visible", as the manual page calls it. > I wouldn't consider it visible if I have to use the fully qualified name > to access it. However if that's the definition, then why are variables > in other namespaces not listed as visible? I can also access those using > their fully qualified name. > > Sorry, those last two paragraphs aren't actually about your tool. They > just came up when trying to resolve the issues flagged by your tool. > > > Schelte. > > > On 07/07/2024 07:43, apnmbx-public--- via Tcl-Core wrote: >> V0.2 of the migration tool has been released following feedback from >> Paul and Andreas (thanks both). >> >> https://github.com/apnadkarni/tcl9-migrate/releases >> <https://github.com/apnadkarni/tcl9-migrate/releases> >> >> Docs: https://github.com/apnadkarni/tcl9-migrate/blob/main/README.md >> >> Additional feedback on false positive/negatives appreciated. >> >> /Ashok > > > > > _______________________________________________ > Tcl-Core mailing list > Tcl...@li... > https://lists.sourceforge.net/lists/listinfo/tcl-core > |