Skip to content
Tcl Source Code Artifact [9dd1632048]
Login

Artifact [9dd1632048]

Artifact 9dd163204800a80be9fd42c19e95bd0d857491919603266d9d2ef2358a5ec7e2:

Wiki page [Migrating scripts to Tcl 9] by oehhar on 2023-11-14 18:41:26.
D 2023-11-14T18:41:26.972
L Migrating\sscripts\sto\sTcl\s9
N text/x-markdown
P 2cb96fb8a7bd5dd074c75b7a60906a506fa8a828c39b27cc17465be56e27d80e
U oehhar
W 10397
Work in progress...
Currently focused on incompatibilities. Possibly hints on improving
scripts making use of new facilities could be added at some point

## Strict encoding

TBD - pending CFV on TIP 657, 653 etc.

## Tilde substitution

File path arguments in Tcl 8 would be subject to tilde substitution
where `~` and `~USER` prefixes in the path would be replaced with the
home directory of the current or named user respectively. Although
convenient for interactive use, this behavior lead to both security and
robustness issues as documented in TIP 602.

Tcl 9 therefore no longer does this substitution and scripts must
explicitly do the same if desired with the new commands `file home` and
`file tildeexpand`. Conversely, commands that had to protect against
this treatment of `~` by prefixing with `./` (`glob`, `file split` etc.)
will no longer to do so. Scripts that had their own workarounds to
protect against tilde substitution no longer need to do so.

As an exception for convenience for end users, tilde substitution is
still done at start up time on environment values containing library
paths (`TCLLIBPATH` and analogous tm path variants).

See [TIP 602](https://core.tcl-lang.org/tips/doc/trunk/tip/602.md).

## Octals

The Tcl 8 intepretation of numeric strings beginning with `0` as octal
representation has been done away with. These are now treated as decimal.
All such uses within a script should instead use the explicit `0o` prefix
for octal notation. Particular care must be taken on input and output
when interacting with programs or users based on the old interpretation.

See [TIP 114](https://core.tcl-lang.org/tips/doc/trunk/tip/114.md).

## Underscores in integers

Tcl 9 allows the use of the `_` underscore character as a separator in
numeric strings, e.g. `1_000_000`, `1_0.0_1`. It is important to note
that this applies not just to literals in source code as in some other
languages but run-time values as well. For example, if the `string is
integer` command is used to validate user input as integers, it will
allow underscores to be entered. If this is not desired, such validation
needs to be changed to use `scan` or regexps.

See [TIP 551](https://core.tcl-lang.org/tips/doc/trunk/tip/551.md).

## Changes in integer classification and handling

The command `string is integer` now accepts integers of any size and is not limited to 32 bit values. Analogously, the `int()` function will no longer truncate integer values to a 32-bit range. The following illustrates the difference.

In Tcl 8,

```
% string is integer 0x100000001
0
% expr {int(0x100000001)}
1
```

In Tcl 9,

```
% string is integer 0x100000001
1
% expr {int(0x100000001)}
4294967297
```

Thus `string is integer` should not be used for range validation; use explicit range checks. Likewise, `int()` cannot be used for truncation; use explicit masking of high order bits.

Further, the commands `string is entier` and `string is wide` and functions `entier()` and `wide()` are deprecated. Again, use explicit range checks and masking instead.

See [TIP 514](https://core.tcl-lang.org/tips/doc/trunk/tip/514.md).

## Change in variable name resolution

In Tcl 8 variable names that are not absolute, are resolved by looking first in the current namespace, and then in the global namespace. In Tcl 9 such variables are always interpreted as relative to the current namespace.

This avoids the problem that setting a variable inside a namespace scope will overwrite a global variable, if that global variable exists and the variable does not exist relative to the namespace. But it also has some consequences that may not immediately be obvious:

* Access to well-known variables such as env and tcl_platform inside a namespace eval block will need to either fully qualified or the variable must be brought into scope using `namespace upvar` command (`namespace upvar :: env env`).
* Inside procs in a namespace ns, it used to be possible to access namespace variables by referring to them as ns::var. That no longer works. Again, the solution is to either fully qualify the variable, or bring it into scope. In addition to the namespace upvar command, in this case that can also be done using the global command (`global ns::var`), as well as the variable command (`variable var`, or `variable ::ns::var`).

Q - has the behavior of `global` changed within namespaces?

The behavior of the `global` command within namespaces has not changed. The command has no effect when used outside the context of a proc or apply body. Using `global` inside a namespace eval block does nothing. Variable names passed to the global command are still resolved relative to the global namespace, as they have always been. So, using `global ns::var` (inside a proc or apply body) will create a local variable var that is linked to the namespace variable ::ns::var.

See [TIP 278](https://core.tcl-lang.org/tips/doc/trunk/tip/278.md).

## Arguments to `load` are Unicode-aware and case sensitive

In Tcl 8, the second argument to the `load` command which specifies
the initialization function was case-insensitive. This is inconsistent
with the rules for package names which are case-sensitive. In Tcl 9,
initialization function names are now case sensitive. For example,
if the initialization function was called `Sample_Init`, either of the
following commands would work in Tcl 8

```
load sample.dll sample
load sample.dll Sample
```

In Tcl 9, only the latter would be successful.

If compatibility with both Tcl 8 and 9 is desired, the following
idiom may be used

```
load sample.dll [string totitle sample]
```

Note that instances of the `load` command that do not have the second
argument specified need not be changed.

In addition, Tcl 9 does not restrict the names to ASCII. In practice
this should not have any compatibility issues.


See [TIP 595](https://core.tcl-lang.org/tips/doc/trunk/tip/595.md).

## Default encoding for scripts is UTF-8

The default encoding used when reading scripts, either with the `source`
command without an explicit `-encoding` option, or passed as a command
line argument to `tclsh` is utf-8 in Tcl 9 instead of the system
encoding as in Tcl 8.

No changes are required for scripts that were pure ASCII and used
the `\u` or \U` escapes for non-ASCII characters. Scripts that were
saved using a (non-utf8) system encoding will have to be modified
or sourced using an explicit `-encoding` option.

See [TIP 587](https://core.tcl-lang.org/tips/doc/trunk/tip/587.md).

## Full Unicode support

TBD - change in behavior of string length etc. for surrogate pairs

See [TIP 497](https://core.tcl-lang.org/tips/doc/trunk/tip/497.md).

## Traces on upvars linked to arrays

In Tcl 8, traces set on an array would not fire when an element of the array
was modified through an `upvar` link. This documented but incongruent
behavior is fixed in Tcl 9. Array traces will fire even when the element
is accessed through an `upvar` link.

See [TIP 634](https://core.tcl-lang.org/tips/doc/trunk/tip/634.md).

## `glob` no longer raises an error if no files match

In Tcl 9, the `glob` command returns an empty list if no files match the
specified pattern instead rasiing an error. This is true irrespective of
the presence of the `-nocomplain` option which is still accepted but has
no significance. Any scripts that expect an error to be raised in such
cases will need to be modified to check for an empty list instead.

See [TIP 637](https://core.tcl-lang.org/tips/doc/trunk/tip/637.md).

## Removal of depreciated commands or arguments

These commands or arguments have been deprecated and have been removed from Tcl 9.
Scripts making use of these should be modified as below:

* Replace `case` with `switch`
* Replace `read -nonewline <chan>` with `read <chan> nonewline`
* Replace `read -nonewline <chan> <line>` with `puts <chan> <line> nonewline`

See [TIP 485](https://core.tcl-lang.org/tips/doc/trunk/tip/485.md)

### Deprecated `trace` subcommands `variable`, `vdelete`, `vinfo` have been removed

These commands have been deprecated since Tcl 8.4 and have been removed from Tcl 9.
Scripts making use of these should be modified as below:

* Replace `trace variable` with `trace add variable`

* Replace `trace vdelete` with `trace remove variable`

* Replace `trace vinfo` with `trace info variable`

See [TIP 673](https://core.tcl-lang.org/tips/doc/trunk/tip/673.md).

## Removal of `tcl_precision`

The long deprecated variable `tcl_precision` that controlled conversion
of floating point numbers to strings has been removed. Use the `format`
command instead to control number of digits generated.

See [TIP 488](https://core.tcl-lang.org/tips/doc/trunk/tip/488.md).

## Zipfs file system

Tcl 9 introduces the ability to embed scripts as a zip archive
bound to an executable or shared library. The default build is configured to make
use of this capability and binds the Tcl initialization and support scripts
into the shared library (for shared builds) or executable (for static builds).
From a scripting perspective, this has several ramifications in terms of
compatibility:

* There is new file system type `zipfs` with its own rules for file names
and directories. In particular, names are case sensitive even on Windows. Other
differences include disallowing of creation of new files and directories. Existing
files may be modified but the changes are not persisted to disk and are lost
after dismounting the archive. See the `zipfs` manpage for details.

* The feature has the side effect of introducing the concept of volumes
on Unix (Windows programmers already had to deal with drives as volumes).

```
        % file volumes
        //zipfs:/ /
```

* One implication of the above is that searches for files can no longer simply begin
at `/` but need to iterate across all volumes.

* The paths within `auto_path` *may* point to locations within the `zipfs` volume.
These are not writable so any installers that copy files into that location will fail.
Installers should check for writable locations in any case but this change will
result in errors in installers that write to directories in `auto_path` without
these checks.

Z 8c0452a00375593f7691f8a27483cc7b