How to use the command 'erl' (with examples)

How to use the command 'erl' (with examples)

The ’erl’ command is used to run and manage programs in the Erlang programming language. It allows you to compile and run Erlang programs, connect to running Erlang nodes, and load modules from directories. This article will provide examples of each of these use cases.

Use case 1: Compile and run sequential Erlang program as a common script and then exit

Code:

erlc files && erl -noshell 'mymodule:myfunction(arguments), init:stop().'

Motivation: This use case is useful when you need to execute a sequential Erlang program as a common script without opening an interactive shell. It allows for a one-time execution of the program and then exits the shell.

Explanation:

  • erlc files: This command compiles Erlang source files into bytecode beam files.
  • &&: This is a shell operator that allows you to chain multiple commands together. In this case, it ensures that the compilation of the Erlang files is completed before running the next command.
  • erl -noshell 'mymodule:myfunction(arguments), init:stop().': This command starts the Erlang runtime system in a non-interactive mode, executes the specified function in the specified module with the provided arguments, and then stops the system.

Example output: The program will be compiled and executed, and the output of the specified function will be displayed.

Use case 2: Connect to a running Erlang node

Code:

erl -remsh nodename@hostname -sname custom_shortname -hidden -setcookie cookie_of_remote_node

Motivation: This use case is helpful when you need to connect to a remote Erlang node to inspect its state, execute commands, and debug issues in a distributed Erlang system.

Explanation:

  • erl -remsh nodename@hostname: This command connects to a running Erlang node specified by its name and host. You need to provide the node name and host to connect to the specific node.
  • -sname custom_shortname: This argument specifies a custom short name for the local Erlang node. It allows for a more user-friendly and meaningful name when connecting to other nodes.
  • -hidden: This argument makes the local Erlang node hidden, meaning it does not show up when other nodes are listing the available nodes.
  • -setcookie cookie_of_remote_node: This argument sets the cookie for the local Erlang node to match the cookie of the remote Erlang node being connected to. The cookie is used for authentication and encryption when establishing connections between nodes.

Example output: Upon successful connection, you will be able to interact with the remote Erlang node’s shell, execute commands, and observe the system’s state.

Use case 3: Tell the Erlang shell to load modules from a directory

Code:

erl -pa directory_with_beam_files

Motivation: This use case is useful when you have additional Erlang modules located in a specific directory and you want to load them into the Erlang shell. Loading the modules allows you to access and use the functionalities defined in those modules.

Explanation:

  • -pa directory_with_beam_files: This argument tells the Erlang shell to add the specified directory to its code path, allowing it to load additional modules from that directory.

Example output: Once the directory is added to the code path, you can import and use the functions defined in the modules located in that directory within the Erlang shell.

Related Posts

How to use the command `fuser` (with examples)

How to use the command `fuser` (with examples)

fuser is a command-line utility that allows you to display process IDs currently using files or sockets.

Read More
A Guide to Using the 1Password Command Line Interface (with examples)

A Guide to Using the 1Password Command Line Interface (with examples)

1Password is a popular password management tool that helps you secure and organize your digital life.

Read More
Using Select-String (with examples)

Using Select-String (with examples)

1: Search for a pattern within a file Select-String -Path "path\to\file" -Pattern 'search_pattern' Motivation: This use case is helpful when you need to search for a specific pattern within a file.

Read More