io
Standard IO Server Interface Functions
This module provides an interface to standard Erlang IO servers.
The output functions all return ok
if they are successful,
or exit if they are not.
这个模块提供了标准Erlang IO服务器的接口. 如果成功的话输出函数都返回ok
, 或者如果不是就退出.
In the following description, all functions have an optional
parameter IoDevice
. If included, it must be the pid of a
process which handles the IO protocols. Normally, it is the
IoDevice
returned by
file:open/2.
在下述描述里, 所有函数都有一个可选的参数IoDevice
. 如果包含这个选项, 它一定是处理IO协议进程的进程号. 通常是file:open/2返回的IoDevice
.
For a description of the IO protocols refer to the STDLIB Users Guide.
关于IO协议的描述, 参阅STDLIB用户指南.
Warning:
As of R13A, data supplied to the put_chars function should be in the
unicode:chardata()
format. This means that programs
supplying binaries to this function need to convert them to UTF-8
before trying to output the data on an
io_device()
.
自从R13A起, 提供给put_chars函数的数据应该是 unicode:chardata()
格式. 这意味在向io_device()
输出数据之前传递给该函数的二进制需要转换成UTF-8.
If an io_device() is set in binary mode, the functions get_chars and get_line may return binaries instead of lists. The binaries will, as of R13A, be encoded in UTF-8.
如果把io_device()设置二进制模式, 函数get_chars 和 get_line 可能返回二进制而不是列表. 从R13A开始二进制数据将会编码成UTF-8.
To work with binaries in ISO-latin-1 encoding, use the file module instead.
对于ISO-latin-1编码的二进制数据, 请使用file模块.
For conversion functions between character encodings, see the unicode module.
关于字符编码的转换函数, 请参阅unicode 模块.
Either standard_io
, standard_error
, a
registered name, or a pid handling IO protocols (returned from
file:open/2).
Whatever the I/O-server sends.
Functions:
columns() -> {'ok', pos_integer()} | {'error', 'enotsup'}
columns(IoDevice) -> {'ok', pos_integer()} | {'error', 'enotsup'}
Retrieves the number of columns of the
(i.e. the width of a terminal). The function
only succeeds for terminal devices, for all other devices
the function returns {error, enotsup}
获取
的列数 (例如终端的宽度). 该函数只对终端设备有效, 对于其它设备则返回{error, enotsup}
put_chars(CharData) -> 'ok'
put_chars(IoDevice, IoData) -> 'ok'
IoData = unicode:chardata()
Writes the characters of
to the io_server()
(
).
向io_server() (
)写字符
.
nl() -> 'ok'
nl(IoDevice) -> 'ok'
Writes new line to the standard output (
).
向标准输出(
)写换行符.
get_chars(Prompt, Count) -> Data | 'eof'
Count = non_neg_integer(),
Data = [unicode:unicode_char()] | unicode:unicode_binary()
get_chars(IoDevice, Prompt, Count) -> Data | 'eof' | {error, Reason}
Prompt = prompt(),
Count = non_neg_integer(),
Reason = term(),
Data = [unicode:unicode_char()] | unicode:unicode_binary()
Reads
characters from standard input
(
), prompting it with
. It
returns:
从标准输入(
)读
个字符, 以
作为提示符. 返回:
Data
The input characters. If the device supports Unicode, the data may represent codepoints larger than 255 (the latin1 range). If the io_server() is set to deliver binaries, they will be encoded in UTF-8 (regardless of if the device actually supports Unicode or not).
输入的字符. 如果设备支持Unicode, 代表codepoints的数据可能大于255(latin1编码范围). 如果io_server()设置为deliver binaries, 它们将被编码成UTF-8(无论设备是否支持Unicode).
eof
End of file was encountered.
遇到文件结尾符.
{error,Reason }
Other (rare) error condition, for instance {error,estale}
if reading from an NFS file system.
其他错误条件(很少遇到), 例如如果从NFS文件系统读时错误{error,estale}
.
get_line(Prompt) -> Data | 'eof' | {'error', Reason}
Reason = term(),
Data = [unicode:unicode_char()] | unicode:unicode_binary()
get_line(IoDevice, Prompt) -> Data | 'eof' | {'error', term()}
Prompt = prompt(),
Data = [unicode:unicode_char()] | unicode:unicode_binary()
Reads a line from the standard input (
),
prompting it with
. It returns:
从标准输入(
)读一行, 以
作为提示符. 返回:
Data
The characters in the line terminated by a LF (or end of file). If the device supports Unicode, the data may represent codepoints larger than 255 (the latin1 range). If the io_server() is set to deliver binaries, they will be encoded in UTF-8 (regardless of if the device actually supports Unicode or not).
由LF或EOF结尾的字符串. 如果设备支持Unicode, 代表codepoints的数据可能大于255(latin1编码范围). 如果io_server()设置为deliver binaries, 它们将被编码成UTF-8(无论设备是否支持Unicode).
eof
End of file was encountered.
遇到文件结尾符.
{error,Reason }
Other (rare) error condition, for instance {error,estale}
if reading from an NFS file system.
其他错误条件(很少遇到), 例如如果从NFS文件系统读时错误{error,estale}
.
getopts() -> [opt_pair()]
getopts(IoDevice) -> [opt_pair()]
This function requests all available options and their current values for a specific io_device(). Example:
1> {ok,F} = file:open("/dev/null",[read]). {ok,<0.42.0>} 2> io:getopts(F). [{binary,false},{encoding,latin1}]
Here the file I/O-server returns all available options for a file,
which are the expected ones, encoding
and binary
. The standard shell however has some more options:
3> io:getopts(). [{expand_fun,#Fun<group.0.120017273>}, {echo,true}, {binary,false}, {encoding,unicode}]
This example is, as can be seen, run in an environment where the terminal supports Unicode input and output.
setopts(Opts) -> 'ok' | {'error', Reason}
Reason = term()
setopts(IoDevice, Opts) -> 'ok' | {'error', Reason}
Opts = [setopt()],
Reason = term()
Set options for the io_device() (
).
Possible options and values vary depending on the actual io_device(). For a list of supported options and their current values on a specific device, use the getopts/1 function.
The options and values supported by the current OTP io_devices are:
binary, list or {binary, boolean()}
If set in binary mode (binary or {binary,true}), the io_server() sends binary data (encoded in UTF-8) as answers to the get_line, get_chars and, if possible, get_until requests (see the I/O protocol description in STDLIB User's Guide for details). The immediate effect is that get_chars/2,3
and get_line/1,2
return UTF-8 binaries instead of lists of chars for the affected device.
By default, all io_devices in OTP are set in list mode, but the io functions can handle any of these modes and so should other, user written, modules behaving as clients to I/O-servers.
This option is supported by the standard shell (group.erl), the 'oldshell' (user.erl) and the file I/O servers.
{echo, boolean()}
Denotes if the terminal should echo input. Only supported for the standard shell I/O-server (group.erl)
{expand_fun, expand_fun()}
Provide a function for tab-completion (expansion)
like the erlang shell. This function is called
when the user presses the Tab key. The expansion is
active when calling line-reading functions such as
get_line/1,2
.
The function is called with the current line, upto
the cursor, as a reversed string. It should return a
three-tuple: {yes|no, string(), [string(), ...]}
. The
first element gives a beep if no
, otherwise the
expansion is silent, the second is a string that will be
entered at the cursor position, and the third is a list of
possible expansions. If this list is non-empty, the list
will be printed and the current input line will be written
once again.
Trivial example (beep on anything except empty line, which is expanded to "quit"):
fun("") -> {yes, "quit", []}; (_) -> {no, "", ["quit"]} end
This option is supported by the standard shell only (group.erl).
{encoding, latin1 | unicode}
Specifies how characters are input or output from or to the actual device, implying that i.e. a terminal is set to handle Unicode input and output or a file is set to handle UTF-8 data encoding.
The option does not affect how data is returned from the io-functions or how it is sent in the I/O-protocol, it only affects how the io_device() is to handle Unicode characters towards the "physical" device.
The standard shell will be set for either unicode or latin1 encoding when the system is started. The actual encoding is set with the help of the "LANG" or "LC_CTYPE" environment variables on Unix-like system or by other means on other systems. The bottom line is that the user can input Unicode characters and the device will be in {encoding, unicode} mode if the device supports it. The mode can be changed, if the assumption of the runtime system is wrong, by setting this option.
The io_device() used when Erlang is started with the "-oldshell" or "-noshell" flags is by default set to latin1 encoding, meaning that any characters beyond codepoint 255 will be escaped and that input is expected to be plain 8-bit ISO-latin-1. If the encoding is changed to Unicode, input and output from the standard file descriptors will be in UTF-8 (regardless of operating system).
Files can also be set in {encoding, unicode}, meaning that data is written and read as UTF-8. More encodings are possible for files, see below.
{encoding, unicode | latin1} is supported by both the standard shell (group.erl including werl on windows), the 'oldshell' (user.erl) and the file I/O servers.
{encoding, utf8 | utf16 | utf32 | {utf16,big} | {utf16,little} | {utf32,big} | {utf32,little}}
For disk files, the encoding can be set to various UTF variants. This will have the effect that data is expected to be read as the specified encoding from the file and the data will be written in the specified encoding to the disk file.
{encoding, utf8} will have the same effect as {encoding,unicode} on files.
The extended encodings are only supported on disk files (opened by the file:open/2 function)
write(Term) -> 'ok'
write(IoDevice, Term) -> 'ok'
Term = term()
Writes the term
to the standard output (
).
向标准输出(
)写项
.
read(Prompt) -> Result
Result = {'ok', Term = term()}
| 'eof'
| {'error', ErrorInfo = erl_scan:error_info()}
read(IoDevice, Prompt) -> Result
Prompt = prompt(),
Result = {'ok', Term = term()}
| 'eof'
| {'error', ErrorInfo = erl_scan:error_info()}
Reads a term
from the standard input
(
), prompting it with
. It
returns:
从标准输入(
)读入一个项
, 提示符为
. 返回:
{ok, Term }
The parsing was successful.
分析正确.
eof
End of file was encountered.
遇到文件结尾符.
{error, ErrorInfo }
The parsing failed.
分析失败.
read(IoDevice, Prompt, StartLine) -> Result
Prompt = prompt(),
StartLine = line(),
Result = {'ok', Term = term(), EndLine = line()}
| {'eof', EndLine = line()}
| {'error', ErrorInfo = erl_scan:error_info(), ErrorLine = line()}
Reads a term
from
, prompting it
with
. Reading starts at line number
. It returns:
{ok, Term, EndLine }
The parsing was successful.
分析正确.
{eof, EndLine }
End of file was encountered.
遇到文件结尾符.
{error, ErrorInfo , ErrorLine }
The parsing failed.
分析失败.
fwrite(Format) -> 'ok'
fwrite(Format, Data) -> 'ok'
Data = [term()]
fwrite(IoDevice, Format, Data) -> 'ok'
Format = format(),
Data = [term()]
format(Format) -> 'ok'
format(Format, Data) -> 'ok'
Data = [term()]
format(IoDevice, Format, Data) -> 'ok'
Format = format(),
Data = [term()]
Writes the items in
([]
) on the standard
output (
) in accordance with
.
contains plain characters which are copied to
the output device, and control sequences for formatting, see
below. If
is an atom or a binary, it is first
converted to a list with the aid of atom_to_list/1
or binary_to_list/1
.
依照
将
([]
)中的项写到标准输出(
).
包含将会拷贝到输出设备的平整(普通)字符, 以及格式控制符, 见下述. 如果
是原子或者二进制数据, 它首先通过atom_to_list/1
或binary_to_list/1
函数进行转换.
1> io:fwrite("Hello world!~n", []).
Hello world!
ok
The general format of a control sequence is ~F.P.PadModC
.
The character C
determines the type of control sequence
to be used, F
and P
are optional numeric
arguments. If F
, P
, or Pad
is *
,
the next argument in Data
is used as the numeric value
of F
or P
.
控制符的格式一般为~F.P.PadModC
. 字符C
决定了控制符的类型, F
和 P
是可选的数字参数. 如果 F
, P
, 或 Pad
是 *
, Data
中的下一个参数作为F
或 P
的数值.
F
is the field width
of the printed argument. A
negative value means that the argument will be left justified
within the field, otherwise it will be right justified. If no
field width is specified, the required print width will be
used. If the field width specified is too small, then the
whole field will be filled with *
characters.
F
是打印参数的字段宽度
. 负数表示该参数是左对齐的, 否则是右对齐. 如果不指定字段宽度, 将会使用需要打印的宽度. 如果指定的字段宽度太小, 那么整个字段将会以*
字符填充.
P
is the precision
of the printed argument. A
default value is used if no precision is specified. The
interpretation of precision depends on the control sequences.
Unless otherwise specified, the argument within
is used
to determine print width.
P
是打印参数的精度
. 如果不指定精度会使用默认值. 精度的解释依赖于控制符. 除非另外指定, 参数within
用来确定打印宽度.
Pad
is the padding character. This is the character
used to pad the printed representation of the argument so that
it conforms to the specified field width and precision. Only
one padding character can be specified and, whenever
applicable, it is used for both the field width and precision.
The default padding character is ' '
(space).
Pad
是填充字符. 这个字符用来填充打印的参数以符合指定的字段宽度和精度. 只能指定一个填充字符, 如果合适的话, 它可用于字段宽度和精度. 默认的填充字符是' '
(空格).
Mod
is the control sequence modifier. It is either a
single character (currently only 't', for unicode translation,
is supported) that changes the interpretation of Data.
Mod
是控制符的修饰符. 它是单个字符(目前只支持用于unicode转换的't'), 可以改变Data的翻译.
The following control sequences are available:
以下控制符可用:
~
The character ~
is written.
输出~
.
c
The argument is a number that will be interpreted as an ASCII code. The precision is the number of times the character is printed and it defaults to the field width, which in turn defaults to 1. The following example illustrates:
2> io:fwrite("|~10.5c|~-10.5c|~5c|~n", [$a, $b, $c]).
| aaaaa|bbbbb |ccccc|
ok
If the Unicode translation modifier ('t') is in effect, the integer argument can be any number representing a valid unicode codepoint, otherwise it should be an integer less than or equal to 255, otherwise it is masked with 16#FF:
1> io:fwrite("~tc~n",[1024]). \x{400} ok 2> io:fwrite("~c~n",[1024]). ^@ ok
f
The argument is a float which is written as
[-]ddd.ddd
, where the precision is the number of
digits after the decimal point. The default precision is 6
and it cannot be less than 1.
e
The argument is a float which is written as
[-]d.ddde+-ddd
, where the precision is the number
of digits written. The default precision is 6 and it
cannot be less than 2.
g
The argument is a float which is written as f
, if
it is >= 0.1 and < 10000.0. Otherwise, it is written
in the e
format. The precision is the number of
significant digits. It defaults to 6 and should not be
less than 2. If the absolute value of the float does not
allow it to be written in the f
format with the
desired number of significant digits, it is also written
in the e
format.
s
Prints the argument with the string
syntax. The
argument is, if no Unicode translation modifier is present, an
iolist(), a binary, or an atom. If the Unicode translation modifier ('t') is in effect, the argument is unicode:chardata(), meaning that binaries are in UTF-8. The characters
are printed without quotes. The string is first truncated
by the given precision and then padded and justified
to the given field width. The default precision is the field width.
This format can be used for printing any object and truncating the output so it fits a specified field:
3> io:fwrite("|~10w|~n", [{hey, hey, hey}]). |**********| ok 4> io:fwrite("|~10s|~n", [io_lib:write({hey, hey, hey})]). |{hey,hey,h| 5> io:fwrite("|~-10.8s|~n", [io_lib:write({hey, hey, hey})]). |{hey,hey | ok
A list with integers larger than 255 is considered an error if the Unicode translation modifier is not given:
1> io:fwrite("~ts~n",[[1024]]).
\x{400}
ok
2> io:fwrite("~s~n",[[1024]]).
** exception exit: {badarg,[{io,format,[<0.26.0>,"~s~n",[[1024]]]},
...
w
Writes data with the standard syntax. This is used to output Erlang terms. Atoms are printed within quotes if they contain embedded non-printable characters, and floats are printed accurately as the shortest, correctly rounded string.
p
Writes the data with standard syntax in the same way as
~w
, but breaks terms whose printed representation
is longer than one line into many lines and indents each
line sensibly. It also tries to detect lists of printable
characters and to output these as strings. For example:
5> T = [{attributes,[[{id,age,1.50000},{mode,explicit},{typename,"INTEGER"}], [{id,cho},{mode,explicit},{typename,'Cho'}]]},{typename,'Person'},{tag,{'PRIVATE',3}},{mode,implicit}]. ... 6> io:fwrite("~w~n", [T]). [{attributes,[[{id,age,1.5},{mode,explicit},{typename, [73,78,84,69,71,69,82]}],[{id,cho},{mode,explicit},{typena me,'Cho'}]]},{typename,'Person'},{tag,{'PRIVATE',3}},{mode ,implicit}] ok 7> io:fwrite("~62p~n", [T]). [{attributes,[[{id,age,1.5}, {mode,explicit}, {typename,"INTEGER"}], [{id,cho},{mode,explicit},{typename,'Cho'}]]}, {typename,'Person'}, {tag,{'PRIVATE',3}}, {mode,implicit}] ok
The field width specifies the maximum line length. It
defaults to 80. The precision specifies the initial
indentation of the term. It defaults to the number of
characters printed on this line in the same
call to
io:fwrite
or io:format
. For example, using
T
above:
8> io:fwrite("Here T = ~62p~n", [T]).
Here T = [{attributes,[[{id,age,1.5},
{mode,explicit},
{typename,"INTEGER"}],
[{id,cho},
{mode,explicit},
{typename,'Cho'}]]},
{typename,'Person'},
{tag,{'PRIVATE',3}},
{mode,implicit}]
ok
W
Writes data in the same way as ~w
, but takes an
extra argument which is the maximum depth to which terms
are printed. Anything below this depth is replaced with
...
. For example, using T
above:
9> io:fwrite("~W~n", [T,9]).
[{attributes,[[{id,age,1.5},{mode,explicit},{typename,...}],
[{id,cho},{mode,...},{...}]]},{typename,'Person'},
{tag,{'PRIVATE',3}},{mode,implicit}]
ok
If the maximum depth has been reached, then it is
impossible to read in the resultant output. Also, the
,...
form in a tuple denotes that there are more
elements in the tuple but these are below the print depth.
P
Writes data in the same way as ~p
, but takes an
extra argument which is the maximum depth to which terms
are printed. Anything below this depth is replaced with
...
. For example:
10> io:fwrite("~62P~n", [T,9]).
[{attributes,[[{id,age,1.5},{mode,explicit},{typename,...}],
[{id,cho},{mode,...},{...}]]},
{typename,'Person'},
{tag,{'PRIVATE',3}},
{mode,implicit}]
ok
B
Writes an integer in base 2..36, the default base is 10. A leading dash is printed for negative integers.
The precision field selects base. For example:
11> io:fwrite("~.16B~n", [31]). 1F ok 12> io:fwrite("~.2B~n", [-19]). -10011 ok 13> io:fwrite("~.36B~n", [5*36+35]). 5Z ok
X
Like B
, but takes an extra argument that is a
prefix to insert before the number, but after the leading
dash, if any.
The prefix can be a possibly deep list of characters or an atom.
14> io:fwrite("~X~n", [31,"10#"]). 10#31 ok 15> io:fwrite("~.16X~n", [-31,"0x"]). -0x1F ok
#
Like B
, but prints the number with an Erlang style
'#'-separated base prefix.
16> io:fwrite("~.10#~n", [31]). 10#31 ok 17> io:fwrite("~.16#~n", [-31]). -16#1F ok
b
Like B
, but prints lowercase letters.
x
Like X
, but prints lowercase letters.
+
Like #
, but prints lowercase letters.
n
Writes a new line.
i
Ignores the next term.
Returns:
ok
The formatting succeeded.
If an error occurs, there is no output. For example:
18> io:fwrite("~s ~w ~i ~w ~c ~n",['abc def', 'abc def', {foo, 1},{foo, 1}, 65]). abc def 'abc def' {foo,1} A ok 19> io:fwrite("~s", [65]). ** exception exit: {badarg,[{io,format,[<0.22.0>,"~s","A"]}, {erl_eval,do_apply,5}, {shell,exprs,6}, {shell,eval_exprs,6}, {shell,eval_loop,3}]} in function io:o_request/2
In this example, an attempt was made to output the single character '65' with the aid of the string formatting directive "~s".
fread(Prompt, Format) -> Result
Format = format(),
Result = {'ok', Terms = [term()]} | 'eof' | {'error', What = term()}
fread(IoDevice, Prompt, Format) -> Result
Prompt = prompt(),
Format = format(),
Result = {'ok', Terms = [term()]} | 'eof' | {'error', What = term()}
Reads characters from the standard input (
),
prompting it with
. Interprets the characters in
accordance with
.
contains control
sequences which directs the interpretation of the input.
may contain:
White space characters (SPACE, TAB and NEWLINE) which cause input to be read to the next non-white space character.
Ordinary characters which must match the next input character.
Control sequences, which have the general format
~*FMC
. The character *
is an optional
return suppression character. It provides a method to
specify a field which is to be omitted. F
is the
field width
of the input field, M
is an optional
translation modifier (of which 't' is the only currently
supported, meaning Unicode translation) and C
determines the type of control sequence.
Unless otherwise specified, leading white-space is ignored for all control sequences. An input field cannot be more than one line wide. The following control sequences are available:
~
A single ~
is expected in the input.
d
A decimal integer is expected.
u
An unsigned integer in base 2..36 is expected. The field width parameter is used to specify base. Leading white-space characters are not skipped.
-
An optional sign character is expected. A sign
character '-' gives the return value -1
. Sign
character '+' or none gives 1
. The field width
parameter is ignored. Leading white-space characters
are not skipped.
#
An integer in base 2..36 with Erlang-style base
prefix (for example "16#ffff"
) is expected.
f
A floating point number is expected. It must follow the Erlang floating point number syntax.
s
A string of non-white-space characters is read. If a field width has been specified, this number of characters are read and all trailing white-space characters are stripped. An Erlang string (list of characters) is returned.
If Unicode translation is in effect (~ts), characters larger than 255 are accepted, otherwise not. With the translation modifier, the list returned may as a consequence also contain integers larger than 255:
1> io:fread("Prompt> ","~s"). Prompt> <Characters beyond latin1 range not printable in this medium> {error,{fread,string}} 2> io:fread("Prompt> ","~ts"). Prompt> <Characters beyond latin1 range not printable in this medium> {ok,[[1091,1085,1080,1094,1086,1076,1077]]}
a
Similar to s
, but the resulting string is
converted into an atom.
The Unicode translation modifier is not allowed (atoms can not contain characters beyond the latin1 range).
c
The number of characters equal to the field width are
read (default is 1) and returned as an Erlang string.
However, leading and trailing white-space characters
are not omitted as they are with s
. All
characters are returned.
The Unicode translation modifier works as with s
:
1> io:fread("Prompt> ","~c"). Prompt> <Character beyond latin1 range not printable in this medium> {error,{fread,string}} 2> io:fread("Prompt> ","~tc"). Prompt> <Character beyond latin1 range not printable in this medium> {ok,[[1091]]}
l
Returns the number of characters which have been scanned up to that point, including white-space characters.
It returns:
{ok, Terms }
The read was successful and
is the list
of successfully matched and read items.
eof
End of file was encountered.
{error, What }
The read operation failed and the parameter
gives a hint about the error.
Examples:
20> io:fread('enter>', "~f~f~f"). enter>1.9 35.5e3 15.0 {ok,[1.9,3.55e4,15.0]} 21> io:fread('enter>', "~10f~d"). enter> 5.67899 {ok,[5.678,99]} 22> io:fread('enter>', ":~10s:~10c:"). enter>: alan : joe : {ok, ["alan", " joe "]}
rows() -> {'ok', pos_integer()} | {'error', 'enotsup'}
rows(IoDevice) -> {'ok', pos_integer()} | {'error', 'enotsup'}
Retrieves the number of rows of the
(i.e. the height of a terminal). The function
only succeeds for terminal devices, for all other devices
the function returns {error, enotsup}
scan_erl_exprs(Prompt) -> Result
Result = erl_scan:tokens_result() | request_error()
scan_erl_exprs(Device, Prompt) -> Result
Prompt = prompt(),
Result = erl_scan:tokens_result() | request_error()
scan_erl_exprs(Device, Prompt, StartLine) -> Result
Prompt = prompt(),
StartLine = line(),
Result = erl_scan:tokens_result() | request_error()
Reads data from the standard input (IoDevice
),
prompting it with Prompt
. Reading starts at line number
StartLine
(1). The data is tokenized as if it were a
sequence of Erlang expressions until a final '.'
is
reached. This token is also returned. It returns:
{ok, Tokens, EndLine}
The tokenization succeeded.
{eof, EndLine}
End of file was encountered.
{error, ErrorInfo, ErrorLine}
An error occurred.
Example:
23> io:scan_erl_exprs('enter>'). enter>abc(), "hey". {ok,[{atom,1,abc},{'(',1},{')',1},{',',1},{string,1,"hey"},{dot,1}],2} 24> io:scan_erl_exprs('enter>'). enter>1.0er. {error,{1,erl_scan,{illegal,float}},2}
scan_erl_form(Prompt) -> Result
Result = erl_scan:tokens_result() | request_error()
scan_erl_form(IoDevice, Prompt) -> Result
Prompt = prompt(),
Result = erl_scan:tokens_result() | request_error()
scan_erl_form(IoDevice, Prompt, StartLine) -> Result
Prompt = prompt(),
StartLine = line(),
Result = erl_scan:tokens_result() | request_error()
Reads data from the standard input (
),
prompting it with
. Starts reading at line number
(1). The data is tokenized as if it were an
Erlang form - one of the valid Erlang expressions in an
Erlang source file - until a final '.'
is reached.
This last token is also returned. The return values are the
same as for scan_erl_exprs/1,2,3
above.
parse_erl_exprs(Prompt) -> Result
Result = parse_ret()
parse_erl_exprs(IoDevice, Prompt) -> Result
Prompt = prompt(),
Result = parse_ret()
parse_erl_exprs(IoDevice, Prompt, StartLine) -> Result
Prompt = prompt(),
StartLine = line(),
Result = parse_ret()
Reads data from the standard input (
),
prompting it with
. Starts reading at line number
(1). The data is tokenized and parsed as if
it were a sequence of Erlang expressions until a final '.' is
reached. It returns:
{ok, ExprList, EndLine}
The parsing was successful.
{eof, EndLine}
End of file was encountered.
{error, ErrorInfo, ErrorLine}
An error occurred.
Example:
25> io:parse_erl_exprs('enter>'). enter>abc(), "hey". {ok, [{call,1,{atom,1,abc},[]},{string,1,"hey"}],2} 26> io:parse_erl_exprs ('enter>'). enter>abc("hey". {error,{1,erl_parse,["syntax error before: ",["'.'"]]},2}
parse_erl_form(Prompt) -> Result
Result = parse_form_ret()
parse_erl_form(IoDevice, Prompt) -> Result
Prompt = prompt(),
Result = parse_form_ret()
parse_erl_form(IoDevice, Prompt, StartLine) -> Result
Prompt = prompt(),
StartLine = line(),
Result = parse_form_ret()
Reads data from the standard input (
),
prompting it with
. Starts reading at line number
(1). The data is tokenized and parsed as if
it were an Erlang form - one of the valid Erlang expressions
in an Erlang source file - until a final '.' is reached. It
returns:
{ok, AbsForm, EndLine}
The parsing was successful.
{eof, EndLine}
End of file was encountered.
{error, ErrorInfo, ErrorLine}
An error occurred.
Standard Input/Output
All Erlang processes have a default standard IO device. This
device is used when no IoDevice
argument is specified in
the above function calls. However, it is sometimes desirable to
use an explicit IoDevice
argument which refers to the
default IO device. This is the case with functions that can
access either a file or the default IO device. The atom
standard_io
has this special meaning. The following example
illustrates this:
27> io:read('enter>'). enter>foo. {ok,foo} 28> io:read(standard_io, 'enter>'). enter>bar. {ok,bar}
There is always a process registered under the name of
user
. This can be used for sending output to the user.
Standard Error
In certain situations, especially when the standard output is redirected, access to an io_server() specific for error messages might be convenient. The io_device 'standard_error' can be used to direct output to whatever the current operating system considers a suitable device for error output. Example on a Unix-like operating system:
$ erl -noshell -noinput -eval 'io:format(standard_error,"Error: ~s~n",["error 11"]),'\'init:stop().' > /dev/null Error: error 11
Error Information
The ErrorInfo
mentioned above is the standard
ErrorInfo
structure which is returned from all IO modules.
It has the format:
{ErrorLine, Module, ErrorDescriptor}
A string which describes the error is obtained with the following call:
Module:format_error(ErrorDescriptor)