Two questions on dynamic code loading example in Joe's book

Kaiduan Xie kaiduanx@REDACTED
Sat Jun 27 02:37:07 CEST 2009


Hi, all,

While trying example from Joe's book on dynamic code loading, I am
puzzled by the following two things,

1. Why we need to compile code under the same erlang shell to make
dynamic loading module b work? If I change b, then compile b under a
unix shell, new code is not loaded dynamically.

kaiduanx@REDACTED:~/joebook/code$ erl
Erlang (BEAM) emulator version 5.5.2 [source] [async-threads:0]
[kernel-poll:false]

Eshell V5.5.2  (abort with ^G)
1> c(b).
{ok,b}
2> c(a).
{ok,a}
3> a:start(one).
<0.43.0>
Vsn1 (one) b:x() = 1
Vsn1 (one) b:x() = 1
Vsn1 (one) b:x() = 1
Vsn1 (one) b:x() = 1
Vsn1 (one) b:x() = 1
Vsn1 (one) b:x() = 1
Vsn1 (one) b:x() = 1
Vsn1 (one) b:x() = 1
Vsn1 (one) b:x() = 1
Vsn1 (one) b:x() = 1

kaiduanx@REDACTED:~/joebook/code$ more b.erl
%% ---
%%  Excerpted from "Programming Erlang",
%%  published by The Pragmatic Bookshelf.
%%  Copyrights apply to this code. It may not be used to create training materia
l,
%%  courses, books, articles, and the like. Contact us if you are in doubt.
%%  We make no guarantees that this code is fit for any purpose.
%%  Visit http://www.pragmaticprogrammer.com/titles/jaerlang for more book infor
mation.
%%---
-module(b).
-export([x/0]).

x() -> 1.


Then I change b.erl from x() -> 1 to x() -> 2, erlc b.erl

The code is not dynamically loaded.

2. If I change module a, even under the same erlang shell, the new
code is still not loaded.

kaiduanx@REDACTED:~/joebook/code$ more a.erl
%% ---
%%  Excerpted from "Programming Erlang",
%%  published by The Pragmatic Bookshelf.
%%  Copyrights apply to this code. It may not be used to create training materia
l,
%%  courses, books, articles, and the like. Contact us if you are in doubt.
%%  We make no guarantees that this code is fit for any purpose.
%%  Visit http://www.pragmaticprogrammer.com/titles/jaerlang for more book infor
mation.
%%---
-module(a).
-compile(export_all).

 start(Tag) ->
    spawn(fun() -> loop(Tag) end).

loop(Tag) ->
    sleep(),
    Val = b:x(),
    io:format("Vsn1 (~p) b:x() = ~p~n",[Tag, Val]),
    loop(Tag).

sleep() ->
    receive
	after 3000 -> true
    end.

After changing to io:format("Vsn2  (~p) b:x() = ~p~n",[Tag, Val]),
even I compile it under the same erlang shell, the new code is still
not loaded.

4> c(a).
{ok,a}
Vsn1 (one) b:x() = 1
Vsn1 (one) b:x() = 1
Vsn1 (one) b:x() = 1
Vsn1 (one) b:x() = 1

Can anyone explain this? Why changing b works but changing a does not work?

Thanks,

kaiduan


More information about the erlang-questions mailing list