file: File Utilities

Table of Content:

Introduction

The file: module provides utilities for manipulating file objects.

Function usages are given in the same format as in the reference doc for the builtin module.

Functions

file:close

~> file:close $file

Closes a file opened with open.

See also open.

file:open

file:open $filename

Opens a file. Currently, open only supports opening a file for reading. File must be closed with close explicitly. Example:

~> cat a.txt
This is
a file.
~> use file
~> f = (file:open a.txt)
~> cat < $f
This is
a file.
~> close $f

See also close.

file:pipe

file:pipe

Create a new Unix pipe that can be used in redirections.

A pipe contains both the read FD and the write FD. When redirecting command input to a pipe with <, the read FD is used. When redirecting command output to a pipe with >, the write FD is used. It is not supported to redirect both input and output with <> to a pipe.

Pipes have an OS-dependent buffer, so writing to a pipe without an active reader does not necessarily block. Pipes must be explicitly closed with prclose and pwclose.

Putting values into pipes will cause those values to be discarded.

Examples (assuming the pipe has a large enough buffer):

~> p = (file:pipe)
~> echo 'lorem ipsum' > $p
~> head -n1 < $p
lorem ipsum
~> put 'lorem ipsum' > $p
~> head -n1 < $p
# blocks
# $p should be closed with prclose and pwclose afterwards

See also prclose and pwclose.

file:prclose

file:prclose $pipe

Close the read end of a pipe.

See also pwclose and pipe.

file:pwclose

file:pwclose $pipe

Close the write end of a pipe.

See also prclose and pipe.