beetlebug▾
/

interp alias

Makes, reads, or removes a command of one interpreter that runs a command of another.

interp alias SRCPATH SRCCMD
interp alias SRCPATH SRCCMD {}
interp alias SRCPATH SRCCMD TARGETPATH TARGETCMD ?ARG ...?

Description

With a target, makes the command SRCCMD in the interpreter of SRCPATH. A call of SRCCMD runs TARGETCMD in the interpreter of TARGETPATH, with the ARG words first and the words of the call after them, and gives its result. TARGETCMD runs at the global level of its interpreter, so a child can run a command of the parent that the child does not have. An alias replaces a command of the same name.

With only SRCPATH and SRCCMD, gives the target words of the alias: TARGETCMD and the ARG words. A name that is no alias gives an empty string. With an empty word in place of the target, removes the alias.

An error of the target is an error of the call in the child, with the message and the -errorcode of the target. The target command is looked up at each call. Two aliases that call each other stop at the nesting limit.

Arguments

ArgumentTypeDefaultDescription
SRCPATHinterpreter pathrequiredThe interpreter of the new command.
SRCCMDcommand namerequiredThe name of the command in that interpreter.
TARGETPATHinterpreter pathnoneThe interpreter that runs the target command, often {} for the current one.
TARGETCMDcommand namenoneThe command that runs for a call.
ARGstringnoneA word that goes before the words of each call.

Returns

SRCCMD for a new alias, the target words for a read, or an empty string for a remove.

Examples

% set c [interp create -safe]; interp alias $c up {} string toupper
up

% interp eval $c {up hello}
HELLO

% interp alias $c up
string toupper

% interp alias $c up {}; interp aliases $c

% interp alias {} count {} llength; count {a b c}
3

Errors

% interp alias $c y nope llength
could not find interpreter "nope"
-errorcode TCL LOOKUP INTERP nope

% interp alias $c a b
wrong # args: should be "interp alias childPath childCmd ?targetPath targetCmd arg ...?"
-errorcode TCL WRONGARGS

# The target is looked up at each call.
% interp alias $c bad {} nosuch; interp eval $c bad
invalid command name "nosuch"
-errorcode TCL LOOKUP COMMAND nosuch

Facts

Capability
interp. A new interpreter does not have it until the host turns it on.
Tcl release
Tcl 9.1 and 8.6.
Since
ztcl 1.0.0-beta.1

See also