From 68d53c01b0b8e9a007a6a30158c19e34b2d2a34e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Gustavsson?= Date: Wed, 18 May 2016 15:53:35 +0200 Subject: Update STDLIB documentation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Language cleaned up by the technical writers xsipewe and tmanevik from Combitech. Proofreading and corrections by Björn Gustavsson and Hans Bolinder. --- lib/stdlib/doc/src/io.xml | 1138 +++++++++++++++++++++++++-------------------- 1 file changed, 634 insertions(+), 504 deletions(-) (limited to 'lib/stdlib/doc/src/io.xml') diff --git a/lib/stdlib/doc/src/io.xml b/lib/stdlib/doc/src/io.xml index 9ae50ed90c..11a64c7f8a 100644 --- a/lib/stdlib/doc/src/io.xml +++ b/lib/stdlib/doc/src/io.xml @@ -29,48 +29,50 @@ io - Standard I/O Server Interface Functions + Standard I/O server interface functions.

This module provides an interface to standard Erlang I/O servers. The output functions all return ok if they are successful, or exit if they are not.

-

In the following description, all functions have an optional + +

All functions in this module have an optional parameter IoDevice. If included, it must be the pid of a - process which handles the IO protocols. Normally, it is the + process that handles the I/O protocols. Normally, it is the IoDevice returned by - file:open/2.

-

For a description of the IO protocols refer to the STDLIB User's Guide.

- - -

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.

- -

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.

+ file:open/2.

-

To work with binaries in ISO-latin-1 encoding, use the file module instead.

- -

For conversion functions between character encodings, see the unicode module.

+

For a description of the I/O protocols, see section + The Erlang I/O Protocol + in the User's Guide.

+ +

As from Erlang/OTP R13A, data supplied to function + put_chars/2 + is to be in the + unicode:chardata() format. This means that programs + supplying binaries to this function must convert them to UTF-8 + before trying to output the data on an I/O device.

+

If an I/O device is set in binary mode, functions + get_chars/2,3 and + get_line/1,2 + can return binaries instead of lists. + The binaries are, as from Erlang/OTP R13A, + encoded in UTF-8.

+

To work with binaries in ISO Latin-1 encoding, use the + file module instead.

+

For conversion functions between character encodings, see the + unicode module.

-
-

An IO device. Either standard_io, standard_error, a - registered name, or a pid handling IO protocols (returned from - file:open/2).

+

An I/O device, either standard_io, standard_error, a + registered name, or a pid handling I/O protocols (returned from + file:open/2). +

@@ -96,7 +98,7 @@ -

What the I/O-server sends when there is no data.

+

What the I/O server sends when there is no data.

@@ -104,329 +106,93 @@ - Get the number of columns of an IO device - -

Retrieves the number of columns of the - IoDevice (i.e. the width of a terminal). The function - only succeeds for terminal devices, for all other IO devices - the function returns {error, enotsup}

-
-
- - - - Write a list of characters - -

Writes the characters of CharData to the I/O server - (IoDevice).

-
-
- - - - Write a newline - -

Writes new line to the standard output (IoDevice).

-
-
- - - - Read a specified number of characters - - -

Reads Count characters from standard input - (IoDevice), prompting it with Prompt. It - returns:

- - Data - -

The input characters. If the IO device supports Unicode, - the data may represent codepoints larger than 255 (the - latin1 range). If the I/O server is set to deliver - binaries, they will be encoded in UTF-8 (regardless of if - the IO device actually supports Unicode or not).

-
- eof - -

End of file was encountered.

-
- {error, ErrorDescription} - -

Other (rare) error condition, for instance {error, estale} - if reading from an NFS file system.

-
-
-
-
- - - - Read a line - - -

Reads a line from the standard input (IoDevice), - prompting it with Prompt. It returns:

- - Data - -

The characters in the line terminated by a LF (or end of - file). If the IO device supports Unicode, - the data may represent codepoints larger than 255 (the - latin1 range). If the I/O server is set to deliver - binaries, they will be encoded in UTF-8 (regardless of if - the IO device actually supports Unicode or not).

-
- eof - -

End of file was encountered.

-
- {error, ErrorDescription} - -

Other (rare) error condition, for instance {error, estale} - if reading from an NFS file system.

-
-
-
-
- - - - Get the supported options and values from an I/O-server + Get the number of columns of an I/O device. -

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.

+

Retrieves the number of columns of the + IoDevice (that is, the width of a terminal). + The function succeeds for terminal devices and returns + {error, enotsup} for all other I/O devices.

- - - Get user requested printable character range - -

Return the user requested range of printable Unicode characters.

-

The user can request a range of characters that are to be considered printable in heuristic detection of strings by the shell and by the formatting functions. This is done by supplying +pc <range> when starting Erlang.

-

Currently the only valid values for <range> are latin1 and unicode. latin1 means that only code points below 256 (with the exception of control characters etc) will be considered printable. unicode means that all printable characters in all unicode character ranges are considered printable by the io functions.

-

By default, Erlang is started so that only the latin1 range of characters will indicate that a list of integers is a string.

-

The simplest way to utilize the setting is to call io_lib:printable_list/1, which will use the return value of this function to decide if a list is a string of printable characters or not.

-

In the future, this function may return more values and ranges. It is recommended to use the io_lib:printable_list/1 function to avoid compatibility problems.

-
-
- - - - Set options - -

Set options for the standard IO device (IoDevice).

-

Possible options and values vary depending on the actual - IO device. For a list of supported options and their current values - on a specific IO 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 I/O 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 IO device.

-

By default, all IO devices in OTP are set in list mode, but the I/O 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 IO 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 I/O 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 IO device will be in {encoding, unicode} mode if the IO 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 a term - -

Writes the term Term to the standard output - (IoDevice).

-
-
- - - Read a term - - -

Reads a term Term from the standard input - (IoDevice), prompting it with Prompt. It - returns:

- - {ok, Term} - -

The parsing was successful.

-
- eof - -

End of file was encountered.

-
- {error, ErrorInfo} - -

The parsing failed.

-
- {error, ErrorDescription} - -

Other (rare) error condition, for instance {error, estale} - if reading from an NFS file system.

-
-
-
-
- - - - Read a term - - -

Reads a term Term from IoDevice, prompting it - with Prompt. Reading starts at location - StartLocation. The argument - Options is passed on as the Options - argument of the erl_scan:tokens/4 function. It returns:

- - {ok, Term, EndLocation} - -

The parsing was successful.

-
- {eof, EndLocation} - -

End of file was encountered.

-
- {error, ErrorInfo, ErrorLocation} - -

The parsing failed.

-
- {error, ErrorDescription} - -

Other (rare) error condition, for instance {error, estale} - if reading from an NFS file system.

-
-
-
-
- - - - - Write formatted output + + + + Write formatted output. -

Writes the items in Data ([]) on the standard - output (IoDevice) in accordance with Format. - Format contains plain characters which are copied to +

Writes the items in Data ([]) on the + standard output (IoDevice) in accordance with + Format. Format contains + plain characters that are copied to the output device, and control sequences for formatting, see - below. If Format 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.

+ below. If Format 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. Example:

 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 +

The general format of a control sequence is ~F.P.PadModC.

+

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 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.

-

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.

-

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).

-

Mod is the control sequence modifier. It is either a - single character (currently only t, for Unicode - translation, and l, for stopping p and - P from detecting printable characters, are supported) - that changes the interpretation of Data.

-

The following control sequences are available:

+ + +

F is the field width of the printed argument. A + negative value means that the argument is left-justified + within the field, otherwise right-justified. If no + field width is specified, the required print width is + used. If the field width specified is too small, the + whole field is filled with * characters.

+
+ +

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, argument within is used + to determine print width.

+
+ +

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).

+
+ +

Mod is the control sequence modifier. It is either a + single character (t, for Unicode + translation, and l, for stopping p and + P from detecting printable characters) + that changes the interpretation of Data.

+
+
+

Available control sequences:

~ -

The character ~ is written.

+

Character ~ is written.

c -

The argument is a number that will be interpreted as an +

The argument is a number that is 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:

+ character is printed and defaults to the field width, + which in turn defaults to 1. Example:

 1> 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 + valid Unicode codepoint, otherwise it is to be an integer less than or equal to 255, otherwise it is masked with 16#FF:

 2> io:fwrite("~tc~n",[1024]).
@@ -435,29 +201,28 @@ ok
 3> io:fwrite("~c~n",[1024]).
 ^@
 ok
-
f -

The argument is a float which is written as +

The argument is a float that 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.

+ and it cannot be < 1.

e -

The argument is a float which is written as +

The argument is a float that 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.

+ cannot be < 2.

g -

The argument is a float which is written as f, if +

The argument is a float that 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 + significant digits. It defaults to 6 and is not to be + < 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.

@@ -471,8 +236,9 @@ ok 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.

+ by the specified precision and then padded and justified to the + specified 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:

@@ -484,7 +250,8 @@ ok
 3> 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:

+

A list with integers > 255 is considered an error if the + Unicode translation modifier is not specified:

 4> io:fwrite("~ts~n",[[1024]]).
 \x{400}
@@ -497,8 +264,8 @@ ok
           
             

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 + they contain embedded non-printable characters. + Floats are printed accurately as the shortest, correctly rounded string.

p @@ -506,11 +273,11 @@ ok

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. Left justification is not supported. + line sensibly. Left-justification is not supported. It also tries to detect lists of printable characters and to output these as strings. The Unicode translation modifier is used for determining - what characters are printable. For example:

+ what characters are printable, for example:

 1> T = [{attributes,[[{id,age,1.50000},{mode,explicit},
 {typename,"INTEGER"}], [{id,cho},{mode,explicit},{typename,'Cho'}]]},
@@ -531,12 +298,13 @@ ok
  {tag,{'PRIVATE',3}},
  {mode,implicit}]
 ok
-

The field width specifies the maximum line length. It - defaults to 80. The precision specifies the initial +

The field width specifies the maximum line length. + 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:

+ characters printed on this line in the same call to + write/1 or + format/1,2,3. + For example, using T above:

 4> io:fwrite("Here T = ~62p~n", [T]).
 Here T = [{attributes,[[{id,age,1.5},
@@ -549,8 +317,8 @@ Here T = [{attributes,[[{id,age,1.5},
           {tag,{'PRIVATE',3}},
           {mode,implicit}]
 ok
-

When the modifier l is given no detection of - printable character lists will take place. For example:

+

When the modifier l is specified, no detection of + printable character lists takes place, for example:

 5> S = [{a,"a"}, {b, "b"}].
 6> io:fwrite("~15p~n", [S]).
@@ -561,9 +329,9 @@ ok
 [{a,[97]},
  {b,[98]}]
 ok
-

Binaries that look like UTF-8 encoded strings will be +

Binaries that look like UTF-8 encoded strings are output with the string syntax if the Unicode translation - modifier is given:

+ modifier is specified:

 9> io:fwrite("~p~n",[[1024]]).
 [1024]
@@ -578,7 +346,7 @@ ok
W

Writes data in the same way as ~w, but takes an - extra argument which is the maximum depth to which terms + extra argument that is the maximum depth to which terms are printed. Anything below this depth is replaced with .... For example, using T above:

@@ -587,17 +355,17 @@ ok
[{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 +

If the maximum depth is reached, it cannot + be 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 + extra argument that is the maximum depth to which terms are printed. Anything below this depth is replaced with - .... For example:

+ ..., for example:

 9> io:fwrite("~62P~n", [T,9]).
 [{attributes,[[{id,age,1.5},{mode,explicit},{typename,...}],
@@ -609,9 +377,9 @@ ok
B -

Writes an integer in base 2..36, the default base is +

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:

+

The precision field selects base, for example:

 1> io:fwrite("~.16B~n", [31]).
 1F
@@ -629,7 +397,7 @@ ok
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.

+ an atom. Example:

 1> io:fwrite("~X~n", [31,"10#"]).
 10#31
@@ -641,7 +409,7 @@ ok
#

Like B, but prints the number with an Erlang style - #-separated base prefix.

+ #-separated base prefix. Example:

 1> io:fwrite("~.10#~n", [31]).
 10#31
@@ -671,14 +439,14 @@ ok

Ignores the next term.

-

Returns:

+

The function returns:

ok

The formatting succeeded.

-

If an error occurs, there is no output. For example:

+

If an error occurs, there is no output. Example:

 1> io:fwrite("~s ~w ~i ~w ~c ~n",['abc def', 'abc def', {foo, 1},{foo, 1}, 65]).
 abc def 'abc def'  {foo,1} A
@@ -692,45 +460,57 @@ ok
      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".

+ "~s".

+ - Read formatted input + Read formatted input. -

Reads characters from the standard input (IoDevice), - prompting it with Prompt. Interprets the characters in - accordance with Format. Format contains control - sequences which directs the interpretation of the input.

-

Format may contain:

+

Reads characters from the standard input + (IoDevice), prompting it with + Prompt. Interprets the characters in accordance + with Format. Format contains + control sequences that directs the interpretation of the input.

+

Format can contain the following:

-

White space characters (SPACE, TAB and NEWLINE) which - cause input to be read to the next non-white space - character.

+

Whitespace characters (Space, Tab, and + Newline) that cause input to be read to the next + non-whitespace character.

-

Ordinary characters which must match the next input +

Ordinary characters that 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 + ~*FMC, where:

+ + +

Character * is an optional return suppression + character. It provides a method to specify a field that + 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 supported, meaning Unicode + translation).

+
+ +

C determines the type of control sequence.

+
+
+

Unless otherwise specified, leading whitespace is ignored for all control sequences. An input field cannot - be more than one line wide. The following control - sequences are available:

+ be more than one line wide.

+

Available control sequences:

~ @@ -742,22 +522,22 @@ ok u -

An unsigned integer in base 2..36 is expected. The +

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.

+ whitespace characters are not skipped.

-

An optional sign character is expected. A sign - character - gives the return value -1. Sign + character - gives return value -1. Sign character + or none gives 1. The field width - parameter is ignored. Leading white-space characters + parameter is ignored. Leading whitespace characters are not skipped.

# -

An integer in base 2..36 with Erlang-style base - prefix (for example "16#ffff") is expected.

+

An integer in base 2-36 with Erlang-style base + prefix (for example, "16#ffff") is expected.

f @@ -766,18 +546,15 @@ ok s -

A string of non-white-space characters is read. If a +

A string of non-whitespace characters is read. If a field width has been specified, this number of - characters are read and all trailing white-space + characters are read and all trailing whitespace 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:

- +

If Unicode translation is in effect (~ts), + characters > 255 are accepted, otherwise + not. With the translation modifier, the returned + list can as a consequence also contain integers > 255:

 1> io:fread("Prompt> ","~s").
 Prompt> <Characters beyond latin1 range not printable in this medium>
@@ -785,22 +562,23 @@ Prompt> <Characters beyond latin1 range not printable in this medium&g
 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).

+

The Unicode translation modifier is not allowed (atoms + cannot 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 + However, leading and trailing whitespace characters are not omitted as they are with s. All characters are returned.

-

The Unicode translation modifier works as with s:

+

The Unicode translation modifier works as with s: +

 1> io:fread("Prompt> ","~c").
 Prompt> <Character beyond latin1 range not printable in this medium>
@@ -808,21 +586,20 @@ Prompt> <Character beyond latin1 range not printable in this medium>
 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 +

Returns the number of characters that have been + scanned up to that point, including whitespace characters.

-

It returns:

+

The function returns:

{ok, Terms} -

The read was successful and Terms is the list - of successfully matched and read items.

+

The read was successful and Terms is + the list of successfully matched and read items.

eof @@ -835,13 +612,14 @@ Prompt> <Character beyond latin1 range not printable in this medium> {error, ErrorDescription} -

The read operation failed and the parameter - ErrorDescription gives a hint about the error.

+

The read operation failed and parameter + ErrorDescription gives a hint about + the error.

-

Examples:

+

Examples:

 20> io:fread('enter>', "~f~f~f").
 enter>1.9 35.5e3 15.0
@@ -854,104 +632,127 @@ enter>:   alan   :   joe
       
     
+
     
-      
-      
-      Get the number of rows of an IO device
+      
+      
+      Read a specified number of characters.
+      
       
-          

Retrieves the number of rows of the - IoDevice (i.e. the height of a terminal). The function - only succeeds for terminal devices, for all other IO devices - the function returns {error, enotsup}

+

Reads Count characters from standard input + (IoDevice), prompting it with + Prompt.

+

The function returns:

+ + Data + +

The input characters. If the I/O device supports Unicode, + the data can represent codepoints > 255 (the + latin1 range). If the I/O server is set to deliver + binaries, they are encoded in UTF-8 (regardless of whether + the I/O device supports Unicode).

+
+ eof + +

End of file was encountered.

+
+ {error, ErrorDescription} + +

Other (rare) error condition, such as {error, estale} + if reading from an NFS file system.

+
+
+ - - - - - Read and tokenize Erlang expressions + + + Read a line. -

Reads data from the standard input (IoDevice), - prompting it with Prompt. Reading starts at location - StartLocation (1). The argument Options - is passed on as the Options argument of the - erl_scan:tokens/4 function. The data is tokenized as if - it were a - sequence of Erlang expressions until a final dot (.) is - reached. This token is also returned. It returns:

+

Reads a line from the standard input (IoDevice), + prompting it with Prompt.

+

The function returns:

- {ok, Tokens, EndLocation} + Data -

The tokenization succeeded.

-
- {eof, EndLocation} - -

End of file was encountered by the tokenizer.

+

The characters in the line terminated by a line feed (or end of + file). If the I/O device supports Unicode, + the data can represent codepoints > 255 (the + latin1 range). If the I/O server is set to deliver + binaries, they are encoded in UTF-8 (regardless of if + the I/O device supports Unicode).

eof -

End of file was encountered by the I/O-server.

+

End of file was encountered.

- {error, ErrorInfo, ErrorLocation} + {error, ErrorDescription} -

An error occurred while tokenizing.

-
- {error, ErrorDescription} - -

Other (rare) error condition, for instance {error, estale} - if reading from an NFS file system.

+

Other (rare) error condition, such as {error, estale} + if reading from an NFS file system.

-

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}
+ - - - - - Read and tokenize an Erlang form - + + + Get the supported options and values from an I/O server. + -

Reads data from the standard input (IoDevice), - prompting it with Prompt. Starts reading - at location StartLocation (1). The - argument Options is passed on as the - Options argument of the erl_scan:tokens/4 - function. 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 dot (.) is reached. - This last token is also returned. The return values are the - same as for scan_erl_exprs/1,2,3 above.

+

Requests all available options and their current + values for a specific I/O device, for 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. + However, the standard shell 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.

+ + + + + Write a newline. + +

Writes new line to the standard output + (IoDevice).

+
+
+ - Read, tokenize and parse Erlang expressions + Read, tokenize, and parse Erlang expressions.

Reads data from the standard input (IoDevice), prompting it with Prompt. Starts reading at location - StartLocation (1). The argument - Options is passed on as the - Options argument of the erl_scan:tokens/4 - function. The data is tokenized and parsed as if it were a - sequence of Erlang expressions until a final dot (.) is reached. - It returns:

+ StartLocation (1). Argument + Options is passed on as argument + Options of function + erl_scan:tokens/4. The data is tokenized and parsed + as if it was a sequence of Erlang expressions until a final dot + (.) is reached.

+

The function returns:

{ok, ExprList, EndLocation} @@ -963,17 +764,17 @@ enter>1.0er. eof -

End of file was encountered by the I/O-server.

+

End of file was encountered by the I/O server.

{error, ErrorInfo, ErrorLocation}

An error occurred while tokenizing or parsing.

- {error, ErrorDescription} - -

Other (rare) error condition, for instance {error, estale} - if reading from an NFS file system.

-
+ {error, ErrorDescription} + +

Other (rare) error condition, such as {error, estale} + if reading from an NFS file system.

+

Example:

@@ -985,24 +786,25 @@ enter>abc("hey".
 {error,{1,erl_parse,["syntax error before: ",["'.'"]]},2}
+ - Read, tokenize and parse an Erlang form + Read, tokenize, and parse an Erlang form.

Reads data from the standard input (IoDevice), prompting it with Prompt. Starts reading at - location StartLocation (1). The argument - Options is passed on as the - Options argument of the erl_scan:tokens/4 - function. 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 dot (.) is reached. It - returns:

+ location StartLocation (1). Argument + Options is passed on as argument + Options of function + erl_scan:tokens/4. The data is tokenized and parsed + as if it was an Erlang form (one of the valid Erlang expressions + in an Erlang source file) until a final dot (.) is reached.

+

The function returns:

{ok, AbsForm, EndLocation} @@ -1014,32 +816,353 @@ enter>abc("hey". eof -

End of file was encountered by the I/O-server.

+

End of file was encountered by the I/O server.

{error, ErrorInfo, ErrorLocation}

An error occurred while tokenizing or parsing.

- {error, ErrorDescription} - -

Other (rare) error condition, for instance {error, estale} - if reading from an NFS file system.

-
+ {error, ErrorDescription} + +

Other (rare) error condition, such as {error, estale} + if reading from an NFS file system.

+
+
+
+
+ + + + Get user-requested printable character range. + +

Returns the user-requested range of printable Unicode characters.

+

The user can request a range of characters that are to be considered + printable in heuristic detection of strings by the shell and by the + formatting functions. This is done by supplying + +pc <range> when starting Erlang.

+

The only valid values for <range> are + latin1 and unicode. latin1 means that only code + points < 256 (except control characters, and so on) + are considered printable. unicode means that all printable + characters in all Unicode character ranges are considered printable + by the I/O functions.

+

By default, Erlang is started so that only the latin1 range + of characters indicate that a list of integers is a string.

+

The simplest way to use the setting is to call + + io_lib:printable_list/1, which uses the return + value of this function to decide if a list is a string of printable + characters.

+ +

In a future release, this function may return more values and + ranges. To avoid compatibility problems, it is recommended to use + function + io_lib:printable_list/1.

+
+
+ + + + + Write a list of characters. + +

Writes the characters of CharData to the I/O + server (IoDevice).

+
+
+ + + + + Read a term. + + +

Reads a term Term from the standard input + (IoDevice), prompting it with + Prompt.

+

The function returns:

+ + {ok, Term} + +

The parsing was successful.

+
+ eof + +

End of file was encountered.

+
+ {error, ErrorInfo} + +

The parsing failed.

+
+ {error, ErrorDescription} + +

Other (rare) error condition, such as {error, estale} + if reading from an NFS file system.

+
+
+
+
+ + + + + Read a term. + + +

Reads a term Term from + IoDevice, prompting it + with Prompt. Reading starts at location + StartLocation. Argument + Options is passed on as argument Options + of function + erl_scan:tokens/4.

+

The function returns:

+ + {ok, Term, EndLocation} + +

The parsing was successful.

+
+ {eof, EndLocation} + +

End of file was encountered.

+
+ {error, ErrorInfo, + ErrorLocation} + +

The parsing failed.

+
+ {error, ErrorDescription} + +

Other (rare) error condition, such as {error, estale} + if reading from an NFS file system.

+
+
+
+
+ + + + + Get the number of rows of an I/O device. + +

Retrieves the number of rows of IoDevice + (that is, the height of a terminal). The function + only succeeds for terminal devices, for all other I/O devices + the function returns {error, enotsup}.

+
+
+ + + + + + + Read and tokenize Erlang expressions. + + +

Reads data from the standard input (IoDevice), + prompting it with Prompt. Reading starts at location + StartLocation (1). Argument Options + is passed on as argument Options of function + + erl_scan:tokens/4. The data is tokenized as if it + were a sequence of Erlang expressions until a final dot (.) is + reached. This token is also returned.

+

The function returns:

+ + {ok, Tokens, EndLocation} + +

The tokenization succeeded.

+
+ {eof, EndLocation} + +

End of file was encountered by the tokenizer.

+
+ eof + +

End of file was encountered by the I/O server.

+
+ {error, ErrorInfo, ErrorLocation} + +

An error occurred while tokenizing.

+
+ {error, ErrorDescription} + +

Other (rare) error condition, such as {error, estale} + if reading from an NFS file system.

+
+
+

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}
+
+
+ + + + + + + Read and tokenize an Erlang form. + + +

Reads data from the standard input (IoDevice), + prompting it with Prompt. Starts reading + at location StartLocation (1). + Argument Options is passed on as argument + Options of function + erl_scan:tokens/4. The data is tokenized as if it + was an Erlang form (one of the valid Erlang expressions in an + Erlang source file) until a final dot (.) is reached. + This last token is also returned.

+

The return values are the same as for + + scan_erl_exprs/1,2,3,4.

+
+
+ + + + + Set options. + +

Set options for the standard I/O device + (IoDevice).

+

Possible options and values vary depending on the + I/O device. For a list of supported options and their current values + on a specific I/O device, use function + getopts/1.

+

The options and values supported by the OTP I/O devices + are as follows:

+ + binary, list, or {binary, boolean()} + +

If set in binary mode (binary or {binary, true}), + the I/O server sends binary data (encoded in UTF-8) as answers + to the get_line, get_chars, and, if possible, + get_until requests (for details, see section + The Erlang I/O Protocol) + in the User's Guide). The immediate effect is that + get_chars/2,3 and + get_line/1,2 + return UTF-8 binaries instead of lists of characters + for the affected I/O device.

+

By default, all I/O devices in OTP are set in list mode. + However, the I/O 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 is to echo input. Only supported for + the standard shell I/O server (group.erl)

+
+ {expand_fun, expand_fun()} + +

Provides 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, up to + the cursor, as a reversed string. It is to 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; the third is a list of + possible expansions. If this list is not empty, + it is printed and the current input line is 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 only supported by the standard shell + (group.erl).

+
+ {encoding, latin1 | unicode} + +

Specifies how characters are input or output from or to the I/O + device, implying that, for example, 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 + I/O functions or how it is sent in the I/O protocol, it only + affects how the I/O device is to handle Unicode characters to the + "physical" device.

+

The standard shell is set for unicode or latin1 + encoding when + the system is started. The 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. + So, the user can input Unicode characters and the I/O device + is in {encoding, unicode} mode if the I/O device supports + it. The mode can be changed, if the assumption of the runtime + system is wrong, by setting this option.

+

The I/O device used when Erlang is started with the "-oldshell" + or "-noshell" flags is by default set to latin1 encoding, + meaning that any characters > codepoint 255 are 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 are 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 has the effect that data is expected to be read as the + specified encoding from the file, and the data is written in the + specified encoding to the disk file.

+

{encoding, utf8} has the same effect as + {encoding, unicode} on files.

+

The extended encodings are only supported on disk files + (opened by function + + file:open/2).

+
+ + + + + Write a term. + +

Writes term Term to the standard output + (IoDevice).

+
+
Standard Input/Output -

All Erlang processes have a default standard IO device. This +

All Erlang processes have a default standard I/O 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 + the function calls in this module. However, it is sometimes desirable to + use an explicit IoDevice argument that refers to the + default I/O device. This is the case with functions that can + access either a file or the default I/O device. The atom standard_io has this special meaning. The following example illustrates this:

+
 27> io:read('enter>').
 enter>foo.
@@ -1047,30 +1170,37 @@ enter>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 I/O-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 IO device for error output. Example on a Unix-like operating system:

+

In certain situations, especially when the standard output is + redirected, access to an I/O server specific for error messages can be + convenient. The I/O device standard_error can be used to direct + output to whatever the current operating system considers a suitable + I/O 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:

+

The ErrorInfo mentioned in this module is the standard + ErrorInfo structure that is returned from all I/O modules. + It has the following format:

+ {ErrorLocation, Module, ErrorDescriptor} -

A string which describes the error is obtained with the following + +

A string that describes the error is obtained with the following call:

+ Module:format_error(ErrorDescriptor)
-- cgit v1.2.3