source(n)
π source(n) β the Tcl Built-In Commands manual, but make it a group chat π
okay bestie, gather round, because this one is genuinely iconic. it's source, it lives in the Tcl Built-In Commands manual, and it's about to become your whole personality. no cap. π«Ά
β¨ NAME β¨
source β slurp up a file or resource and evaluate it as a Tcl script. that's it. that's the girl. π
πΈ SYNOPSIS πΈ
source fileName
source -encoding encodingName fileName
two forms, both slay. the first is the classic, the second lets you tell it what encoding to expect. π
π¦ DESCRIPTION π¦
okay so. source takes the entire contents of the file or resource you point it at and hands that whole thing to the Tcl interpreter like "here, read this as a text script." π
and the return value? it's the return value of the last command executed in the script. so whatever that final command spat out, that's what source hands back to you. periodt.
now for the plot twists:
- if an error happens while evaluating the contents of the script, then
sourcewill return that error. not hide it, not swallow it β return it. π - if a
returncommand gets invoked from inside the script, then the remainder of the file is skipped, andsourcereturns normally with the result from thatreturncommand. so it's a graceful exit, not a crash. main character energy. π
π the end-of-file character situation
the end-of-file character for files is "\32" (that's ^Z) for all platforms. yes, all of them. source will read files up to this character. π
heads up though: that restriction does not exist for thereadorgetscommands, which is what allows for files containing code and data segments β aka scripted documents. so if you need that flexibility,read/getsare your girls.
and if you genuinely require a "^Z" inside code for string comparison, use "\x1A" instead β it'll be safely substituted by the Tcl interpreter into "^Z". sneaky but valid. π
π§ BOM business
a leading BOM (Byte order mark) contained in the file is ignored for unicode encodings β that's utf-8, utf-16, and ucs-2. so don't stress about it. β¨
π the -encoding option
the -encoding option is how you specify the encoding of the data stored in fileName. π
and when you just... don't mention -encoding at all? the utf-8 encoding is assumed. default behaviour, we love her.
π₯ EXAMPLE π₯
run the script in the file foo.tcl, and then the script in the file bar.tcl:
source foo.tcl
source bar.tcl
or, alternatively, if you want to be fancy about it:
foreach scriptFile {foo.tcl bar.tcl} {
source $scriptFile
}
the second one is giving efficiency. the first one is giving "I have two files and I'm not sorry." both valid. β
π SEE ALSO π
go read those too, they're all in the same friend group. π«Ά
π KEYWORDS π
file, script
iykyk. π
and that's the whole page, bestie β brought to you by the Tcl manual, source(n), straight out of the Tcl Built-In Commands collection. no version stamp or date on this one, just vibes and a very well-behaved interpreter. go forth and source something. πβ¨π
π¦ where this page comes from
- alpine 3.23: tcl9-doc 9.0.3-r0 Β· TCL
- alpine 3.24: tcl9-doc 9.0.3-r2 Β· TCL
- fedora 43: tcl-doc 1:9.0.2-1.fc43 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- fedora 44: tcl-doc 1:9.0.2-1.fc44 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- nixos 26.05: tcl 9.0.3
- nixpkgs unstable: tcl 9.1.0
- Copyright (c) 1993 The Regents of the University of California
- Copyright (c) 1994-1996 Sun Microsystems, Inc
- Copyright (c) 2000 Scriptics Corporation
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms