io_lib(3erl)
π io_lib(3erl): Erlang module definition, part 1 π
Okay bestie, this is the io_lib(3erl) page, part 1, and it is very Erlang-core. Buckle in π
β¨ NAME β¨
io_lib β I/O library functions. Thatβs the name, periodt.
π¦ DESCRIPTION π¦
This module contains functions for converting to and from strings (lists of characters), bestie. They are used for implementing the functions in the io module. There is no guarantee that the character lists returned from some of the functions are flat β they can be deep lists. Function lists:flatten/1 can be used for flattening deep lists. Slay.
π§ DATA TYPES π§
chars() = [char() | chars()]β deep-list character energy.continuation()β a continuation as returned byfread/3.chars_limit() = integer()depth() = -1 | integer() >= 0fread_error() = atom | based | character | float | format | input | integer | string | unsignedfread_item() = string() | atom() | integer() | float()latin1_string() = [unicode:latin1_char()]
Hereβs the big one, format_spec():
format_spec() =
#{control_char := char(),
args := [any()],
width := none | integer(),
adjust := left | right,
precision := none | integer(),
pad_char := char(),
encoding := unicode | latin1,
strings := boolean()}
Where:
control_charis the type of control sequence:$P,$w, and so on.argsis a list of the arguments used by the control sequence, or an empty list if the control sequence does not take any arguments.widthis the field width.adjustis the adjustment.precisionis the precision of the printed argument.pad_charis the padding character.encodingis set totrueif translation modifiertis present.stringsis set tofalseif modifierlis present.
π₯ EXPORTS π₯
πΈ build_text(FormatList) -> chars() πΈ
Types:
FormatList = [char() | format_spec()]
For details, see scan_format/2. Thatβs the whole vibe here π«Ά
πΈ char_list(Term) -> boolean() πΈ
Types:
Term = term()
Returns true if Term is a flat list of characters in the Unicode range, otherwise false. Flat-list check, iconic π
πΈ deep_char_list(Term) -> boolean() πΈ
Types:
Term = term()
Returns true if Term is a, possibly deep, list of characters in the Unicode range, otherwise false. Deep-list realness β¨
πΈ deep_latin1_char_list(Term) -> boolean() πΈ
Types:
Term = term()
Returns true if Term is a, possibly deep, list of characters in the ISO Latin-1 range, otherwise false. Latin-1 but make it deep π
πΈ format(Format, Data) -> chars() / fwrite(Format, Data) -> chars() πΈ
Types:
Format = io:format()
Data = [term()]
Returns a character list that represents Data formatted in accordance with Format. For a detailed description of the available formatting options, see io:fwrite/1,2,3. If the format string or argument list contains an error, a fault is generated. Not cute, but important.
If and only if the Unicode translation modifier is used in the format string (that is, ~ts or ~tc), the resulting list can contain characters beyond the ISO Latin-1 character range (that is, numbers > 255). If so, the result is still an ordinary string(), and can well be used in any context where Unicode data is allowed. Unicode girlies, this oneβs for you π
πΈ format(Format, Data, Options) -> chars() / fwrite(Format, Data, Options) -> chars() πΈ
Types:
Format = io:format()
Data = [term()]
Options = [Option]
Option = {chars_limit, CharsLimit}
CharsLimit = chars_limit()
Returns a character list that represents Data formatted in accordance with Format in the same way as fwrite/2 and format/2, but takes an extra argument, a list of options. Extra options? We love a customizable queen π
Valid option:
{chars_limit, CharsLimit}: a soft limit on the number of characters returned. When the number of characters is reached, remaining structures are replaced by"...".CharsLimitdefaults to-1, which means no limit on the number of characters returned.
πΈ fread(Format, String) -> Result πΈ
Types:
Format = String = string()
Result =
{ok, InputList :: [fread_item()], LeftOverChars ::
string()} |
{more,
RestFormat :: string(),
Nchars :: integer() >= 0,
InputStack :: chars()} |
{error, {fread, What :: fread_error()}}
Tries to read String in accordance with the control sequences in Format. For a detailed description of the available formatting options, see io:fread/3. It is assumed that String contains whole lines. Whole lines only, bestie β¨
The function returns:
{ok, InputList, LeftOverChars}: the string was read.InputListis the list of successfully matched and read items, andLeftOverCharsare the input characters not used.{more, RestFormat, Nchars, InputStack}: the string was read, but more input is needed to complete the original format string.RestFormatis the remaining format string,Ncharsis the number of characters scanned, andInputStackis the reversed list of inputs matched up to that point.{error, What}: the read operation failed and parameterWhatgives a hint about the error. A little hint, okay? π
Example:
3> io_lib:fread("~f~f~f", "15.6 17.3e-6 24.5").
{ok,[15.6,1.73e-5,24.5],[]}
πΈ fread(Continuation, CharSpec, Format) -> Return πΈ
Types:
Continuation = continuation() | []
CharSpec = string() | eof
Format = string()
Return =
{more, Continuation1 :: continuation()} |
{done, Result, LeftOverChars :: string()}
Result =
{ok, InputList :: [fread_item()]} |
eof |
{error, {fread, What :: fread_error()}}
This is the re-entrant formatted reader. The continuation of the first call to the functions must be []. For a complete description of how the re-entrant input scheme works, see Armstrong, Virding, Williams: 'Concurrent Programming in Erlang', Chapter 13. Very academic, very chic π
The function returns:
{done, Result, LeftOverChars}: the input is complete. The result is one of the following:{ok, InputList}: the string was read.InputListis the list of successfully matched and read items, andLeftOverCharsare the remaining characters.eof: end of file was encountered.LeftOverCharsare the input characters not used.{error, What}: an error occurred and parameterWhatgives a hint about the error.
{more, Continuation}: more data is required to build a term.Continuationmust be passed tofread/3when more data becomes available.
πΈ indentation(String, StartIndent) -> integer() πΈ
Types:
String = string()
StartIndent = integer()
Returns the indentation if String has been printed, starting at StartIndent. Spacing detective behavior π
πΈ latin1_char_list(Term) -> boolean() πΈ
Types:
Term = term()
Returns true if Term is a flat list of characters in the ISO Latin-1 range, otherwise false. Flat and Latin-1, no cap π
πΈ nl() -> string() πΈ
Returns a character list that represents a new line character. New line, new me β¨
πΈ print(Term) -> chars() / print(Term, Column, LineLength, Depth) -> chars() πΈ
Types:
Term = term()
Column = LineLength = integer() >= 0
Depth = depth()
Returns a list of characters that represents Term, but breaks representations longer than one line into many lines and indents each line sensibly. Also tries to detect and output lists of printable characters as strings. Itβs giving formatting fairy π§
Columnis the starting column; defaults to1.LineLengthis the maximum line length; defaults to80.Depthis the maximum print depth; defaults to-1, which means no limitation.
πΈ printable_latin1_list(Term) -> boolean() πΈ
Types:
Term = term()
Returns true if Term is a flat list of printable ISO Latin-1 characters, otherwise false. Printable Latin-1 vibes only π
πΈ printable_list(Term) -> boolean() πΈ
Types:
Term = term()
Returns true if Term is a flat list of printable characters, otherwise false.
What is a printable character in this case is determined by startup flag +pc to the Erlang VM; see io:printable_range/0 and erl(1). VM says whatβs printable, iykyk π
πΈ printable_unicode_list(Term) -> boolean() πΈ
Types:
Term = term()
Returns true if Term is a flat list of printable Unicode characters, otherwise false. Unicode printables, we love to see it π
πΈ scan_format(Format, Data) -> FormatList πΈ
Types:
Format = io:format()
Data = [term()]
FormatList = [char() | format_spec()]
Returns a list corresponding to the specified format string, where control sequences have been replaced with corresponding tuples. This list can be passed to:
build_text/1to have the same effect asformat(Format, Args)unscan_format/1to get the corresponding pair ofFormatandArgs(with every*and corresponding argument expanded to numeric values)
A typical use of this function is to replace unbounded-size control sequences like ~w and ~p with the depth-limited variants ~W and ~P before formatting to text in, for example, a logger. Protect your logs, bestie π«Ά
πΈ unscan_format(FormatList) -> {Format, Data} πΈ
Types:
FormatList = [char() | format_spec()]
Format = io:format()
Data = [term()]
For details, see scan_format/2. Thatβs it, thatβs the function π
πΈ write(Term) -> chars() / write(Term, Depth) -> chars() / write(Term, Options) -> chars() πΈ
Types:
Term = term()
Options = [Option]
Option =
{chars_limit, CharsLimit} |
{depth, Depth} |
{encoding, latin1 | utf8 | unicode}
CharsLimit = chars_limit()
Depth = depth()
β¦and we're back, bestie π
So this one takes a Term and hands you a character list that represents it. Option Depth controls how deep the structures get written, and the way that works is: once you hit the specified depth, everything below that level gets swapped out for "...". Depth defaults to -1, which lowkey means no limitation at all. Then there's option CharsLimit, which puts a soft limit on the number of characters returned β when you hit that number of characters, the remaining structures get replaced by "..." too. CharsLimit defaults to -1, which again means no limit on how many characters come back.
Example, because examples ate and left no crumbs:
1> lists:flatten(io_lib:write({1,[2],[3],[4,5],6,7,8,9})).
"{1,[2],[3],[4,5],6,7,8,9}"
2> lists:flatten(io_lib:write({1,[2],[3],[4,5],6,7,8,9}, 5)).
"{1,[2],[3],[...],...}"
3> lists:flatten(io_lib:write({[1,2,3],[4,5],6,7,8,9}, [{chars_limit,20}])).
"{[1,2|...],[4|...],...}"
π write_atom(Atom) -> chars() π
Types:
Atom = atom()
Returns the list of characters you need to print out the atom Atom. That's it, that's the function π«Ά
πΈ write_atom_as_latin1(Atom) -> latin1_string() πΈ
Types:
Atom = atom()
Same deal β returns the list of characters needed to print atom Atom β but non-Latin-1 characters get escaped. It's giving "same energy, stricter dress code" β¨
π¦ write_char(Char) -> chars() π¦
Types:
Char = char()
Returns the list of characters needed to print a character constant in the Unicode character set π
π write_char_as_latin1(Char) -> latin1_string() π
Types:
Char = char()
Returns the list of characters needed to print a character constant in the Unicode character set β but once again, non-Latin-1 characters are escaped. Same tea, latin1 cup π΅
β write_latin1_char(Latin1Char) -> latin1_string() β
Types:
Latin1Char = unicode:latin1_char()
Returns the list of characters needed to print a character constant in the ISO Latin-1 character set. Note it's Latin-1 specifically, no Unicode shenanigans here, iykyk π
π§ write_latin1_string(Latin1String) -> latin1_string() π§
Types:
Latin1String = latin1_string()
Returns the list of characters needed to print Latin1String as a string. Iconic, straightforward, no notes π
π write_string(String) -> chars() π
Types:
String = string()
Returns the list of characters needed to print String as a string. Periodt π
π write_string_as_latin1(String) -> latin1_string() π
Types:
String = string()
Returns the list of characters needed to print String as a string β with the usual caveat that non-Latin-1 characters are escaped. Bestie, if you've read one _as_latin1 you've read them all, but we're listing every single one because that's the assignment π«‘
Quick vibe check reminder: everywrite_*function here just builds the character list for you β it doesn't print anything to your terminal. You want output, you flatten it and hand it toio:format/2yourself. β¨
And that's the whole page, bestie β go forth and write your terms π This lil doc dropped as part of Ericsson AB's stdlib 4.3.1.3 release, living in io_lib(3erl). Slay, read responsibly, and don't forget to lists:flatten ππ¦π
π¦ where this page comes from
- ubuntu noble: erlang-manpages 1:25.3.2.8+dfsg-1ubuntu4 Β· Apache-2.0
- ubuntu noble: erlang-manpages 1:25.3.2.8+dfsg-1ubuntu4.8 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms