Hi!
On Wed, Aug 8, 2012 at 6:43 PM, Jai <
j...@vidyamantra.com> wrote:
> I am new to both Sock.js and Erlang. I am trying to run a example.
> $ ./deps/sockjs/examples/cowboy_echo.erl
> escript: exception error: undefined function sockjs_handler:init_state/4
> in function cowboy_echo:main/1 (./deps/sockjs/examples/cowboy_echo.erl,
> line 17)
> in call from escript:run/2 (escript.erl, line 727)
> in call from escript:start/1 (escript.erl, line 277)
> in call from init:start_it/1
> in call from init:start_em/1
>
> Are there any pointers that can help me?
Okay. Welcome to Erlang.
In Erlang, as in other programming languages, programmer is responsible
for pointing to the virtual machine (aka: interpreter) where the program files
are.
By typing:
$ ./deps/sockjs/examples/cowboy_echo.erl
You're saying: dear bash, please run this file.
Bash looks at the header and sees:
#!/usr/bin/env escript
Cool. Escript is executed with this script. Escript then runs erlang
vm. It sees some configuration options in the file:
%%! -smp disable +A1 +K true -pa ebin deps/cowboy/ebin -input
Those are options for the erlang vm. They set, among other
things, the "-pa" path: "-pa ebin deps/cowboy/ebin"
In other words it tells erlang that other runnable files are
in the "ebin" and "deps/cowboy/ebin" directories.
In the traceback you pointed, erlang is complaining that
it can't find the method: "sockjs_handler:init_state"
As you can imagine, sockjs_handler is not in "ebin" nor in "deps/cowboy".
It is in "deps/sockjs/ebin".
What you could do - copy the script and modify "-pa" option to point
to proper dirs.
But here's the problem - using "escript" and hardcoded
directories is not really the idiomatic way on how to
run erlang programs. We're using escript for simplicity
only.
"Properly" running erlang programs is a completely different
topic. You might want to write a proper "otp-ish application",
and so on. Take a look at the epic tutorial from Richard Jones
(where "rebar" is covered):
http://www.metabrew.com/article/erlang-rebar-tutorial-generating-releases-upgrades
As this is a larger topic I suggest reading a book on Erlang.
Marek