Skip to content

Basic IO

tim-hardcastle edited this page Aug 29, 2023 · 17 revisions

All Charm scripts automatically import a library called world.ch. This contains some commands and types that let you perform basic IO.

It does this in ways that are entirely extensible and customizable: you can use Charm to talk to whatever applications or devices your OS can talk to. We will discuss how to write libraries for such purposes in the section on Advanced Charm. For now, let's look at the contents of the built-in world library.

get

get commands perform input. They typically take the form get <variable name> from <source>.

  • get x from File(<string>) sets x equal to the contents of the given file.
  • get x from File(<string>, <type>) sets x equal to the contents of the given file as a string if the type is string, and broken into a list of strings at the newlines of the file if the type is list.
  • get x from FileExists(<string>) sets x equal to true or false depending on whether the file does in fact exist.
  • get x from Input(<string>) sets x equal to the response we get when prompting the end-user with the given string.
  • get x from Random(<integer>) sets x equal to a random integer less than the supplied integer.
  • get x from Random(<list>) sets x equal to a random element of the list.
  • get x from UnixClock(<TimeUnit>) sets xequal to the Unix epoch in the given unit, whereTimeUnitis an enum with valuesSECONDS, MILLISECONDS, and NANOSECONDS`.

put, post, and delete

Clone this wiki locally