unknown(n)

πŸ’… unknown(n) β€” Tcl Built-In Commands

okay bestie, pull up a chair πŸͺ‘ because this one is literally the "who dis??" of the Tcl world and honestly it's giving main character energy.


✨ NAME ✨

unknown β€” the lil handler that catches you when you try to run a command that does not exist 🫠


πŸŽ€ SYNOPSIS πŸŽ€

unknown cmdName ?arg arg ...?

🌸 DESCRIPTION 🌸

so here's the tea πŸ“ β€” this command gets invoked by the Tcl interpreter whenever a script tries to invoke a command that does not exist. the default implementation of unknown is a library procedure that gets defined when Tcl initializes an interpreter. you can override the default unknown to change what it does, or you can register a whole new handler for individual namespaces using the namespace unknown command. note: there is no default implementation of unknown in a safe interpreter. πŸ’€

if the Tcl interpreter runs into a command name that has no defined command (like, neither in the current namespace nor the global namespace), then Tcl checks whether there's an unknown handler for the current namespace. by default that handler is a command named ::unknown. if there's no such command, the interpreter just returns an error. πŸ™ˆ

if the unknown command does exist (or a new handler has been registered for the current namespace), then it gets invoked with arguments consisting of the fully-substituted name and arguments for the original non-existent command. the unknown command typically does stuff like searching through library directories for a command procedure with the name cmdName, or expanding abbreviated command names to full-length, or automatically executing unknown commands as sub-processes. in some cases (like when it's expanding abbreviations) unknown will change the original command slightly and then (re-)execute it. and the result of the unknown command is used as the result for the original non-existent command. slay, honestly. ✨

πŸ¦‹ the default behavior, explained πŸ¦‹

the default implementation of unknown behaves like this, okay:

  1. it first calls the auto_load library procedure to load the command. if that succeeds, it executes the original command with its original arguments. 🫢
  2. if the auto-load fails and Tcl is running interactively, then unknown calls auto_execok to see if there's an executable file by the name cmd. if so, it invokes the Tcl exec command with cmd and all the args as arguments. πŸ”₯
  3. if cmd can't be auto-executed, unknown checks whether the command was invoked at top-level and outside of any script. if so, unknown takes two additional steps:
    • first, it sees if cmd has one of the following three forms: !!, !event, or ^old^new?^?. if so, then unknown carries out history substitution in the same way that csh would for these constructs. πŸ‘‘
    • finally, unknown checks whether cmd is a unique abbreviation for an existing Tcl command. if so, it expands the command name and executes the command with the original arguments. ⭐
  4. if none of the above efforts has been able to execute the command, unknown generates an error return. 😭

also, heads up:

  • if the global variable auto_noload is defined, then the auto-load step is skipped.
  • if the global variable auto_noexec is defined then the auto-exec step is skipped.

under normal circumstances the return value from unknown is the return value from the command that was eventually executed. periodt. πŸ’…


🌈 EXAMPLE 🌈

arrange for the unknown command to have its standard behavior, except first it logs the fact that a command was not found:

# Save the original one so we can chain to it
rename unknown _original_unknown

# Provide our own implementation
proc unknown args {
    puts stderr "WARNING: unknown command: $args"
    uplevel 1 [list _original_unknown {*}$args]
}

not us writing a whole wrapper so we can gossip about every typo 😭 iconic.


πŸ’œ SEE ALSO πŸ’œ

info(n), proc(n), interp(n), library(n), namespace(n)


πŸŽ€ KEYWORDS πŸŽ€

error, non-existent command, unknown


fr fr bestie, remember: unknown is your safety net, not your vibe-ruiner. it catches the command that never was and gives it a whole second chance. 🧚

this has been unknown(n) from the Tcl Built-In Commands manual β€” Tcl, unknown(n). mwah, go forth and handle your non-existent commands, queen πŸ‘‘πŸ’–

πŸ“¦ where this page comes from