Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the easiest way for beginners to install a module?

Tags:

erlang

I checked out rebar, but that seems way too complex. Maybe someone could post something that says, %Post your code here in the following rebar app:

%%%-------------------------------------------------------------------
%% @doc myapp public API
%% @end
%%%-------------------------------------------------------------------

-module('myapp_app').

-behaviour(application).

%% Application callbacks
-export([start/2
        ,stop/1]).

%%====================================================================
%% API
%%====================================================================

start(_StartType, _StartArgs) ->
    'myapp_sup':start_link().

%%--------------------------------------------------------------------
stop(_State) ->
    ok.

Is there some easier alternative for beginners?

like image 646
7stud Avatar asked Dec 15 '15 00:12

7stud


4 Answers

You cannot escape having to learn your language's build system if you're going to create anything non-trivial. If you are familiar with GNU Make you can use erlang.mk instead of Rebar, but I believe Rebar3 is the most beginner-friendly.


To solve the specific problem of running someone's library code inside of the REPL, try the following with rebar3:

rebar3 new app

Ignore the source files that it creates for now. Edit rebar.config and add dependencies. Then, launch a REPL that has those dependencies loaded (along with any source files you've created in the src/ directory):

rebar3 shell

like image 146
Nathaniel Waisbrot Avatar answered Nov 06 '22 21:11

Nathaniel Waisbrot


I don't think there is an easier way. Learning rebar looks like the logical step here. I also think that there is a lots of effort went into rebar and rebar3 to mare Erlang more appealing to newcomers.

like image 36
Istvan Avatar answered Nov 06 '22 21:11

Istvan


I began learning erlang last year and what I found really helpful to get familiar with erlang was writing code in a simple notepad of your choice, and saving it as myapp_app.erl. Then I would open up the erlang console, and use the compile command, c, to compile my module which would then let me call my functions.

> c(myapp_app).
> myapp_app:start(a,b).
like image 44
liam_g Avatar answered Nov 06 '22 21:11

liam_g


Here's what I ended up doing:

Install rebar3:

$ git clone https://github.com/rebar/rebar3.git
$ cd rebar3
.../rebar3$ ./bootstrap

And now you have the script rebar3 and can copy it to somewhere in your $PATH as described in the previous section.

Or, you can download the .zip file, unzip it, then change into the rebar3 directory and issue the ./bootstrap command.

That creates the executable rebar3 inside the /rebar3 directory. I moved the rebar3 executable into /usr/local/bin, which is a directory listed in my PATH environment variable (various possible PATH locations are discussed here):

.../rebar3$ sudo mv rebar3 /usr/local/bin

That allows me to use the rebar3 command at any prompt. As an alternative, after you create your app(see the next step) you can copy rebar3 into your app's directory and from that directory issue rebar commands like this:

$ ./rebar3 .....

Then I created a new app:

~/erlang_programs$ rebar3 new app myapp

(Substitute your app name in place of: myapp)

Then:

~/erlang_programs$ cd myapp
~/erlang_programs/myapp$ 

In order to use jsx, a third party JSON library, I added jsx as a dependency:

Dependencies are listed in rebar.config file under the deps key:

{erl_opts, [debug_info]}.
{deps, [
  {jsx, "2.8.0"}
]}.

rebar.config resides here:

~/erlang_programs/myapp$ ls
LICENSE     _build      rebar.lock
README.md   rebar.config    src

Then I added my erlang program to the myapp/src directory:

my_app/src/my.erl:

-module(my).
-export([test/0]).

test() ->
    jsx:decode(<<"{\"data\": [1, 2, 3]}">>).

Finally:

~/erlang_programs/myapp$ rebar3 shell

===> Verifying dependencies...
===> Fetching jsx ({pkg,<<"jsx">>,<<"2.8.0">>})
===> Version cached at /Users/7stud/.cache/rebar3/hex/default/packages/jsx-2.8.0.tar is up to date, reusing it
===> Compiling jsx
===> Compiling myapp

Erlang/OTP 17 [erts-6.4] [source] [64-bit] [smp:4:4] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V6.4  (abort with ^G)
1> my:test().
[{<<"data">>,[1,2,3]}]

If you make changes to your .erl file, in my case the file my.erl, you don't need to exit the shell--just compile as usual, e.g. c(my).

like image 39
7stud Avatar answered Nov 06 '22 21:11

7stud