A subcommand of interp that is used to create and manage aliases, both within an interpreter (great for shortcuts) and between interpreters (vital for safe tcl). Modes of operation are:
interp alias srcPath srcCmd
interp alias srcPath srcCmd {}
interp alias srcPath srcCmd targetPath targetCmd ?arg arg ...?
interp aliases ?path?
Very often used to introduce command aliases inside the current interpreter (see Custom curry), where the alias target may well be a sequence of words:
interp alias {} strlen {} string length
The case where source and target have identical signatures is Tcl's idiomatic way to write a command alias:
interp alias {} new_name {} existing_command
This can be used with some punctuation to make code shorter and potentially more readable as long as the reader knows about the alias(es) in effect.
interp alias {} @ {} lindex
To delete such an alias again, just do:
interp alias {} @ {}
See Transliteration of how to use an alias, besides its original function, as a kind of global data storage
Does anybody else here have the guts to admit to using echo instead of puts as often as possible? 8-)
Erm ... nope.
RS doesn't, but if you accept the puts API (only one string argument), here goes:
interp alias {} echo {} puts
See also:
Cleverness due to dgp (and others):
package require Tcl 8.5 interp alias {} err {} return -code error -level 0
More generally, currying (see custom curry) in other languages has these analogues, as mentioned above.
Also clever is the use to which Rohan Pall puts aliases on his home Wiki page.
RS: interp alias is a big sugar provider for doing OO - to be able to call a method in the usual
$object method arg...
way, it's sufficient to alias that to a dispatcher (with the "self" pointer as first argument):
interp alias {} $object {} ${class}::dispatch $object
LES: This little thing puzzles me:
interp alias {} exists {} file exists
Then [ exists hi.txt ] returns 1. The file is there.
But [ file exists hi.txt ] also returns 1. How does it avoid looping forever into [file exists] = [file file exists] = [file file file exists] = [file file file file exists]...
schlenk: Because an alias is a command and this is only significant in the first word. The command for file is just 'file' and its first argument is 'exists'. So there is no loop. (But interp alias has loop detection built in...)
SYStems Okay, since the example below from Brent Welch's book doesn't loop forever, we can assume the alias open doesn't shadow the hidden command open; where is this documented? I think I searched everywhere, plus I played around this a little, and this seem kind of inconsistant, as in how come slave interp still think open is hidden, when it gets an exposed alias called open. The hidden command open should be renamed first, then replaced by the alias open; the example below should have raised the error invalid hidden command name "open", since slave now have an alias called open; open should have been remove from its list of interp hidden, right or what?
# Example 19-6 # Substitutions and hidden commands. # interp alias slave open {} safeopen slave proc safeopen {slave filename {mode r}} { # do some checks,then... interp invokehidden $slave open $filename $mode } interp eval slave {open \[exit\]}
RS The open command for the slave is "safeopen" in the master. This way, the master's open is not lost.
jcw - The following might be surprising:
% interp alias {} pass {} return pass % pass 1 1 % proc a {b} { pass $b; return 2 } % a 3 3 %
Explanation: an alias is not the same thing as wrapping a call in a proc.
Here's a question: is there an alias which does act as a no-op? I'm looking for something like:
% interp alias {} pass {} ...? pass % pass 1 1 % proc a {b} { pass $b; return 2 } % a 3 2 %
Extra requirement: pass must return its argument. Only trick I can think of is with a dummy variable:
interp alias {} pass {} set a-name-which-is-never-used-anywhere
MM This may work:
interp alias {} pass {} format %s
but one-and-only-one argument is mandatory.
MG My understanding of interp alias is that it just creates a 'pointer' to another command, and when the alias is called it runs the command it 'points' to instead, with the same args. So what you need is a command which takes a string (as its last argument), and returns that string unchanged, without having any side effects (such as return's side effect of stopping execution of the current proc, etc). Apart from
format %s
mentioned above, you could also use
interp alias {} pass {} subst -nocommand -novariable -nobackslash
but that also only takes one arg. A few very basic tests of
interp alias {} pass {} concat
seem to work with multiple args, though you have to properly protect them by quoting, or the concat affects list-nesting (which has an effect on strings containing brace characters), which isn't desirable at all. The easiest way by far, though, is to not use interp alias, but a proc:
proc pass {args} {return $args}
which you can then alias later, if you want to be able to call it by multiple names - but then, where's the fun in that? :) I expect RS or someone else will come up with many different ways that I've not thought of. :)
jcw - Thanks. Single arg suits me just fine. I'm looking for the fastest no-op (have not done any timing measurements yet). One which can be replaced later in the app when needed, but which has minimal impact when left as is. The "format %s" trick is nice, but has as drawback that it loses the dual-object representation of its argument. Yes, proc pass is an option, clearly.
DKF: In 8.5, the best solution is:
interp alias {} pass {} return -level 0
RS proposes this (can do multiple args, does not change internal rep):
% interp alias {} pass {} list pass % pass 1 1 % pass 2 3 4 2 3 4
MG One thing to beware of, with both the proc version above and RS's alias of list, is that (when doing multiple args), the quoting is likely to be altered. For instance (using either the proc/list methods):
% pass this is "a test" this is {a test}
With the concat alias, it produces
% pass this is "a test" this is a test
because it concats the lists - but with that, at least, you can use
% pass this is {"a test"} this is "a test"
They still don't quite work correctly with multiple args, as they are, though.
See also proc-local alias