Erlang - sgml/signature GitHub Wiki

Hello World

-module(my_module).
-export([my_function/1]).

my_function(X) ->
    io:format("Hello, ~p!~n", [X]).

Compilation

$ erl
Erlang/OTP 23 [erts-11.1.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe] [dtrace]

1> c(my_module).
{ok,my_module}

Command Line Interface

2> my_module:my_function("world").
Hello, "world"!
ok

Process Registration

Pid = spawn(fun() -> receive stop -> ok end end),
register(my_process, Pid).

Regex Example

-module(phone_validator).
-export([validate_phone_number/1]).

validate_phone_number(Number) ->
    Regex = "\\(?\\d{3}\\)?[ .-]?\\d{3}[ .-]?\\d{4}",
    re:run(Number, Regex, [{capture, first, list}]).

Signal Handler from Python

Python

import os
import signal
import subprocess

def handle_sighup(signum, frame):
    # Send a message to the Erlang process
    subprocess.run(["erl", "-noshell", "-sname", "signal_handler", "-s", "my_module", "handle_sighup", str(erlang_pid)])

# Start the Erlang process
erlang_process = subprocess.Popen(["erl", "-noshell", "-s", "my_module", "start"])
erlang_pid = erlang_process.pid

# Set up the signal handler for SIGHUP
signal.signal(signal.SIGHUP, handle_sighup)

# Keep the Python script running
try:
    erlang_process.wait()
except KeyboardInterrupt:
    pass

Erlang

-module(my_module).
-export([start/0, handle_sighup/1]).

start() ->
    register(sighup_handler, self()),
    loop().

loop() ->
    receive
        {sighup} ->
            io:format("Received SIGHUP signal~n"),
            loop();
        _ ->
            loop()
    end.

handle_sighup(Pid) ->
    {ok, Pid} = string:to_integer(Pid),
    erlang:send(Pid, {sighup}),
    halt().

Tutorials

Examples

Glossary

Fault Tolerant: We create a single actor for each functional task, and each actor or thread has it's own supervisor thread. If an actor fails to execute it's task, it suspends itself and all of its children and sends an exception to its supervisor. The supervisor can then work on a recovery strategy. Actors and supervisors fail gracefully, and all the failures can be ultimately managed by the Elixir/Erlang virtual machine (called BEAM).

Scalable: Because the system has a set of dedicated actors for each functional module and the actors have very little overhead associated with them, it is easy to spawn new actors with an increase in the workload for any specific module. The number of actors is only limited by the resources available on the physical machine.

Supervisor: a process that supervises other processes called child processes. A child process can either be another supervisor or a worker process. Worker processes are normally implemented using one of the gen_event, gen_server, or gen_statem behaviors.

Exceptions: A process will crash and restart after an unhandled exception. Processes can monitor other processes and detect process terminations. A run-time error can also be emulated by calling erlang:error(Reason) or erlang:error(Reason, Args).

BIF: Built-in Function

A Built-in Function(BIF) is the opposite of a User Defined Function (UDF)

Macros

Macros are global flags. For example, erlc -DNOASSERT=true *.erl disables assertions

OTP: Open Telecom Platform

References

Open Source Integrations