programming erlang ch6 - andstudy/forge GitHub Wiki

์–ผ๋žญ ์…ธ ์ค‘๋‹จํ•˜๊ธฐ

  • ^G๋ˆ„๋ฅด๊ณ  (Windows์—์„œ๋Š” ^Break) A ๋ˆ„๋ฅด๊ธฐ
  • erlang:halt() - ์‹œ์Šคํ…œ์„ ์ฆ‰๊ฐ ์ค‘๋‹จ์‹œํ‚ค๋Š” BIF. ์˜ค๋ฅ˜ ๋ณต๊ตฌ ์ ˆ์ฐจ ํ•„์š”
  • q()

๊ฐœ๋ฐœ ํ™˜๊ฒฝ ์ˆ˜์ •ํ•˜๊ธฐ

์ฝ”๋“œ ๋กœ๋”ฉ ๊ฒ€์ƒ‰ ๊ฒฝ๋กœ ์„ค์ •

๋กœ๋“œ๋˜์ง€ ์•Š์€ ์–ด๋–ค ๋ชจ๋“ˆ์˜ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๊ฒฝ์šฐ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜๊ณ  ์‹œ์Šคํ…œ์€ ํ˜„์žฌ ๋กœ๋“œ ๊ฒฝ๋กœ์— ๋“ค์–ด์žˆ๋Š” ๋ชจ๋“  ๋””๋ ‰ํ† ๋ฆฌ์—์„œ ํ•ด๋‹น ๋ชจ๋“ˆ์˜ ๋ชฉ์  ์ฝ”๋“œ ํŒŒ์ผ์„ ๊ฒ€์ƒ‰ํ•œ๋‹ค.

    > code:get_path().
    > code:add_patha(Dir).
    > code:add_pathz(Dir).
    > code:all_loaded().
    > code:clash().
    -----------------------------------------------------
    $ erl -pa Dir1 -pa Dir2 ... -pz DirK1 -pz DirK2

์–ผ๋žญ ๋Ÿฐํƒ€์ž„ ์‹œ์Šคํ…œ์€ ์–ผ๋žญ์œผ๋กœ ๋งŒ๋“ค์–ด์ง„ ์ฝ”๋“œ ์ž๋™ ๋กœ๋“œ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ์‚ฌ์šฉํ•œ๋‹ค. E.4์ ˆ '๋™์  ์ฝ”๋“œ ๋กœ๋”ฉ'(483์ชฝ) ์ฐธ๊ณ 

์‹œ์Šคํ…œ์ด ์‹œ์ž‘ํ•  ๋•Œ ๋ช…๋ น ์‹คํ–‰

  • ํ™ˆ ๋””๋ ‰ํ† ๋ฆฌ์˜ .erlang ํŒŒ์ผ์— ์–ผ๋žญ ์ฝ”๋“œ ์ž‘์„ฑ

  • ํ˜„์žฌ ๋””๋ ‰ํ† ๋ฆฌ์˜ .erlang ํŒŒ์ผ์ด ์šฐ์„  - ์–ด๋””์„œ ์‹œ์ž‘ํ•˜๋А๋ƒ์— ๋”ฐ๋ผ ์–ผ๋žญ์ด ๋‹ค๋ฅธ ๋ฐฉ์‹์œผ๋กœ ์ž‘๋™ํ•˜๋„๋ก ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

  • ์–ผ๋žญ์ด ํŒ๋‹จํ•˜๋Š” ํ™ˆ ๋””๋ ‰ํ† ๋ฆฌ ๋ณด๊ธฐ

      1> init:get_argument(home).
      {ok,[["/user1/bt22d"]]}
    

ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•˜๋Š” ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•๋“ค

    -module(hello).
    -export([start/0]).
    
    start() ->
        io:format("Hello world~n").

์–ผ๋žญ ์…ธ์—์„œ ์ปดํŒŒ์ผํ•˜๊ณ  ์‹คํ–‰ํ•˜๊ธฐ

    [bt22d@fog:/user1/bt22d/STUDY/erlang 11 ] erl
    Erlang (BEAM) emulator version 5.6.3 [source] [smp:4] [async-threads:0] [hipe] [kernel-poll:false]
    
    Running Erlang DIR(erlang)
    Eshell V5.6.3  (abort with ^G)
    1> c(hello).
    {ok,hello}
    2> hello:start().
    Hello world
    ok

๋ช…๋ น ํ”„๋กฌํ”„ํŠธ์—์„œ ์ปดํŒŒ์ผํ•˜๊ณ  ์‹คํ–‰ํ•˜๊ธฐ

    [bt22d@fog erlang]$ erlc hello.erl
    [bt22d@fog erlang]$ erl -noshell -s hello start -s init stop
    Hello world
    [bt22d@fog erlang]$

-noshell : ์ธํ„ฐ๋ ‰ํ‹ฐ๋ธŒ ์…ธ ์—†์ด ์–ผ๋žญ ์‹œ์ž‘ -s hello start : hello:start() ํ•จ์ˆ˜ ์‹คํ–‰ (์ปดํŒŒ์ผ๋˜์–ด ์žˆ์–ด์•ผ ํ•จ) -s init stop : init:stop() ์‹คํ–‰ (-s ... ๋ช…๋ น์€ apply๋ฌธ๊ณผ ํ•จ๊ป˜ ์ˆ˜ํ–‰๋˜๋ฉฐ ์™„๋ฃŒ ์‹œ ๊ทธ ๋‹ค์Œ ๋ช…๋ น์ด ์ˆ˜ํ–‰๋œ๋‹ค)

Escript๋กœ ์‹คํ–‰ํ•˜๊ธฐ

    #!/usr/bin/env escript
    
    main(_) ->
        io:format("Hello world\n").

    [bt22d@fog erlang]$ chmod u+x hello
    [bt22d@fog erlang]$ ./hello
    Hello world
    [bt22d@fog erlang]$

๋ช…๋ นํ–‰ ์ธ์ˆ˜๋ฅผ ๊ฐ€์ง€๋Š” ํ”„๋กœ๊ทธ๋žจ

    -module(fac1).
    -export([main/1]).
    
    main([A]) ->
        I = list_to_integer(atom_to_list(A)),
        F = fac(I),
        io:format("factorial ~w = ~w~n",[I, F]),
        init:stop().
    
    fac(0) -> 1;
    fac(N) -> N*fac(N-1).

Makefile๋กœ ์ปดํŒŒ์ผ ์ž๋™ํ™”ํ•˜๊ธฐ

    # leave these lines alone
    .SUFFIXES: .erl .beam .yrl
    
    .erl.beam:
        erlc -W $<
    
    .yrl.erl:
        erlc -W $<
    
    ERL = erl -boot start_clean
    
    # ์ปดํŒŒ์ผ ํ•˜๋ ค๋Š” ์–ผ๋žญ ๋ชจ๋“ˆ๋“ค์˜ ๋ชฉ๋ก
    # If the modules don't fit onto one line add a \ character
    # to the end of the line and continue on the next line
    
    # Edit the lines below
    MODS = module1 module2 \
           module3 ... special1 ...\
           ...
           moduleN
    
    # The first target in any makefile is the default target.
    # If you just type "make" then "make all" is assumed (because
    #   "all" is the first target in this makefile)
    
    all: compile
    
    compile: ${MODS:%=%.beam} subdirs
    
    ## special compilation requirements are added here
    
    special1.beam: special1.erl
        ${ERL} -Dflag1 -W0 special1.erl
    
    ## run an application from the makefile
    
    application1: compile
        ${ERL} -pa Dir1  -s application1 start Arg1 Arg2
    
    # the subdirs target compiles any code in
    # sub-directories
    
    subdirs:
        cd dir1; make
        cd dir2; make
        ...
    
    # remove all the code
    
    clean:
        rm -rf *.beam erl_crash.dump
        cd dir1; make clean
        cd dir2; make clean

์–ผ๋žญ ์…ธ์—์„œ ๋ช…๋ น ํŽธ์ง‘ํ•˜๊ธฐ

| ^A | ์ค„์˜ ์‹œ์ž‘ | | ^E | ์ค„์˜ ๋ | | ^F | ํฌ์›Œ๋“œ ๋ฌธ์ž | | ^B | ๋ฐฑ์›Œ๋“œ ๋ฌธ์ž | | ^P | ์ด์ „ ์ค„ | | ^N | ๋‹ค์Œ ์ค„ | | ^T | ๋งˆ์ง€๋ง‰ ๋‘ ๋ฌธ์ž ๊ต์ฒด | | Tab | ํ˜„์žฌ์˜ ๋ชจ๋“ˆ ๋˜๋Š” ํ•จ์ˆ˜ ์ด๋ฆ„์˜ ํŽผ์นจ์„ ์‹œ๋„ |

์–ผ๋žญ์„ ์ค‘๋‹จํ•˜๊ธฐ ์–ด๋ ค์šด ๊ฒฝ์šฐ

  • ์…ธ์ด ์‘๋‹ตํ•˜์ง€ ์•Š๋Š”๋‹ค.
  • ^C ํ•ธ๋“ค๋Ÿฌ๊ฐ€ ๋น„ํ™œ์„ฑํ™”
  • -detached ํ”Œ๋ž˜๊ทธ๋กœ ์–ผ๋žญ์„ ์‹œ์ž‘ํ•œ ๊ฒฝ์šฐ
  • -heart Cmd ์˜ต์…˜์œผ๋กœ ์–ผ๋žญ์„ ์‹œ์ž‘ํ•œ ๊ฒฝ์šฐ
  • ์ข€๋น„ ์–ผ๋žญ ํ”„๋กœ์„ธ์Šค

๋ญ”๊ฐ€ ์ž˜๋ชป๋˜์—ˆ์„ ๋•Œ

์ •์˜๊ฐ€ ์•ˆ๋œ ์ฝ”๋“œ

    [bt22d@fog:/user1/bt22d 49 ] erl -pa "/user1/bt22d/STUDY/erlang"
    Erlang (BEAM) emulator version 5.6.3 [source] [smp:4] [async-threads:0] [hipe] [kernel-poll:false]
    
    Running Erlang
    Eshell V5.6.3  (abort with ^G)
    1> code:clash().
    ** /user1/bt22d/STUDY/erlang/a.beam hides ./a.beam
    ** Found 1 name clashes in code paths
    ok
    2>

Makefile์ด make๋˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ

์…ธ์ด ์‘๋‹ตํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ

์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ž‘๋™ํ•˜๋Š” ์…ธ์„ ๋‘๊ณ  ์›ํ•˜๋Š” ๋ช…๋ น์„ ๋‚ด๋ฆด ์ˆ˜ ์žˆ๋‹ค.

  • ๋ฌดํ•œํžˆ ๊ธฐ๋‹ค๋ฆฌ๋Š” ์…ธ์„ ์ค‘๋‹จ์‹œํ‚ค๊ธฐ

      1> receive foo -> true end.
      
      User switch command
       --> h
        c [nn]   - connect to job
        i [nn]   - interrupt job
        k [nn]   - kill job
        j        - list all jobs
        s        - start local shell
        r [node] - start remote shell
        q        - quit erlang
        ? | h    - this message
       --> j
         1* {shell,start,[init]}
       --> s
       --> j
         1  {shell,start,[init]}
         2* {shell,start,[]}
       --> c 2
      Eshell V5.6.3  (abort with ^G)
      1> init:stop().
      ok
      2> [bt22d@fog:/user1/bt22d/STUDY/erlang 27 ]
      [bt22d@fog:/user1/bt22d/STUDY/erlang 27 ]
    

๋„์›€ ๋ฐ›๊ธฐ

  • ์œ ๋‹‰์Šค ์‹œ์Šคํ…œ์—์„œ ๋„์›€๋ง ๋ณด๊ธฐ

      $ erl -man erl
      ...
    
  • ๊ฐœ๋ณ„ ๋ชจ๋“ˆ ๋„์›€๋ง

       $ erl -man lists
       ...
    
  • ๋„์›€๋ง ๋ฌธ์„œ

  • Windows์—์„œ๋Š” ๊ธฐ๋ณธ ์„ค์น˜, Unix ์‹œ์Šคํ…œ์—์„œ๋Š” ์„ค์น˜ ํ•„์š”

ํ™˜๊ฒฝ ๊ฐœ์กฐํ•˜๊ธฐ

์–ผ๋žญ ์…ธ์— ๋‚ด์žฅ๋œ ๋ช…๋ น๋“ค์€ ์…ธ ๋ช…๋ น help()๋ฅผ ์ด์šฉํ•ด์„œ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์ด ๋‚ด์žฅ ๋ช…๋ น๋“ค์€ shell_default ๋ชจ๋“ˆ์— ์ •์˜๋˜์–ด ์žˆ๋‹ค.

    2> help().
    ** shell internal commands **
    b()        -- display all variable bindings
    e(N)       -- repeat the expression in query <N>
    f()        -- forget all variable bindings
    f(X)       -- forget the binding of variable X
    h()        -- history
    ...
  • user_default ๋ชจ๋“ˆ์„ ๋งŒ๋“ค๋ฉด ์‚ฌ์šฉ์ž ๋‚ด์žฅ ๋ช…๋ น์„ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. user_default.beam์ด ๋กœ๋“œ ๊ฒฝ๋กœ ์–ด๋”˜๊ฐ€ ์œ„์น˜ํ•œ๋‹ค๋ฉด ๋ชจ๋“ˆ ์ด๋ฆ„ ์—†์ด ํ˜ธ์ถœ ๊ฐ€๋Šฅ

      %%------------------
      %% user_default.erl
      %%------------------
      
      -module(user_default).
      
      -compile(export_all).
      
      hello() ->
          "Hello Joe how are you?".
      
      away(Time) ->
          io:format("Joe is away and will be back in ~w minutes~n",
                [Time]).
    

ํฌ๋ž˜์‰ฌ ๋คํ”„

  • erl_crash.dump

      -module(test).
      -export([crash/0]).
      
      crash() ->
        1000000 * crash().
    
  • ์›น ๊ธฐ๋ฐ˜์˜ ํฌ๋ž˜์‹œ ๋ถ„์„๊ธฐ

      1> webtool:start().
      WebTool is available at http://localhost:8888/
      Or  http://127.0.0.1:8888/
      {ok,<0.34.0>}
    
  • http://localhost:8888/

โš ๏ธ **GitHub.com Fallback** โš ๏ธ