write()
Write text to a file path
Pass a path when you want the runtime to manage opening and closing.
use fs
path = fs.temporary_file()
try:
write(path, 'Atom')
print(read(path))
finally:
fs.remove(path)
Atom
Writing to the path replaces its contents.
Send a message through a channel
When the destination is a channel, write() enqueues a copied value.
use task
queue = task.channel(1)
try:
write(queue, 7)
print(read(queue))
finally:
task.close(queue)
7
The reader receives the value sent through the queue.
Writes to a path, file, socket or channel.
Signature
write(destination, value[, timeout])
Parameters
Text path, live writable file, connected socket, or channel. Use text for a text-mode file. Binary file handles, paths and sockets accept text or a packed value's bytes. Channel value must be transferable. Optional timeout is supported for channels. Sockets use their configured timeout. Requires libuv.
Returns
Unsigned byte count for byte destinations, or true after channel send.
Behavior
A path write replaces the file and closes it. A handle keeps its open mode and advances. A socket sends bytes. A channel enqueues a copy.
Errors
Permission, closed or incompatible handle, I/O failure, timeout, or nontransferable message.
Example
use task
q = task.channel()
assert(write(q, 7) and read(q) == 7)
task.close(q)