Put¶
Status: Stable
documented, exercised by the test suite and/or worked examples, with no known limitations recorded.
Description¶
Put[expr, "filename"] or expr >> "filename"
writes expr to the file, replacing any prior contents.
Put[expr1, expr2, ..., "filename"]
writes a sequence of expressions to the file, each followed by a newline.
Put["filename"]
creates an empty file with the specified name (or truncates an existing one).
Notes
expr \>\> "filename" is equivalent to Put\[expr, "filename"\]; the bare-word form expr \>\> filename is equivalent to expr \>\> "filename". Returns Null on success and $Failed if the file cannot be opened.Examples (1)¶
Every input below was run against the current Mathilda build and its output recorded.
Applications (1)¶
Options & behaviour¶
Parser notes¶
>>has a low precedence (30) — belowSet/SetDelayed(40) and aboveCompoundExpression(10).a + b >> "f"therefore parses asPut[a + b, "f"].- The bare-word filename accepts identifier characters plus
.,/,\,-,_,~, and$.
Algorithm¶
readwrite.c - File I/O builtins (Get, Put).
Get reads Mathilda source from a file and evaluates each expression, returning the last value (used by the REPL bootstrap to load the internal .m initialization files).
Put writes one or more expressions to a file in InputForm so the
infix shorthand expr >> "file" and lowers it to Put[expr, "file"].
Implementation notes¶
builtin_put (and builtin_putappend) share put_common, which treats the last argument as the filename and writes every preceding expression to it, one per line. Put opens the file in "w" mode (truncating); each expr_i is serialized with expr_to_string (the standard printer) followed by \n. The single-argument form Put["file"] runs the loop zero times, leaving an empty file. Open failure prints Put::noopen and returns $Failed; success returns the symbol Null. The output is intended to be re-readable with Get. ATTR_PROTECTED. The infix expr >> "file" lowers to Put[expr, "file"].
Protected.- The last argument must be a string; it is interpreted as a filename.
- Each
expr_iis rendered with the standard printer (the same form used at the REPL) and followed by a single\n. - Truncates the file before writing — preserves nothing from a prior
Put/PutAppend. - Returns
Nullon success and$Failedon I/O error.
Attributes: Protected.
References¶
See also: PutAppend, Set, SetDelayed, CompoundExpression
- Source:
src/readwrite.c - Specification:
docs/spec/builtins/file-io.md - Tests:
tests/test_readwrite.c
Notes & additional examples¶
Notes¶
Put[expr, "file"] (equivalently expr >> "file") writes expr to the file, replacing any prior contents. Read it back with Get.