[erlang-questions] module_info and code switch

Szoboszlay Dániel dszoboszlay@REDACTED
Wed Nov 27 00:06:22 CET 2013


Hi,

If you compile the module with the 'E' option you can see the actual code  
generated for module_info/0,1:

module_info() ->
erlang:get_module_info(loopie).

module_info(X) ->
erlang:get_module_info(loopie, X).

This means the compiler doesn't implement the function by hard coding the  
results, but via caling a BIF instead. And the BIF always uses the  
current  version of the module. Which, I personally think, is OK and  
consistent with the rest of OTP: if you would e.g. print the result of  
code:get_object_code(?MODULE) it would also reflect the current version.

However, I think this behaviour of module_info is not documented properly.  
The "compiler will hard code the results in the module" would be a valid  
mental model too, especially since erlang:get_module_info/1,2 functions  
aren't documented either. So I'd raise a bug report on the documentation.

BR,
Daniel

On Tue, 26 Nov 2013 19:43:09 -0000, Fred Youhanaie <fly@REDACTED>  
wrote:

> Hi
>
> I'm playing with a simple piece of code where I switch versions via erl 
> shell.
>
> Curiously I noticed that as soon as I load a new version of the module, 
> module_info/1 returns the attribute of the currently loaded version 
> rather than the version for the running process!
>
> Is this intentional?
>
> Code and sample session is at the bottom.
>
> Thanks
>
> Fred
>
> Here's the code:
>
> -module(loopie).
> -compile([export_all]).
> start() ->
> start(5000).
> start(T) ->
> register(loopie, spawn(?MODULE, loop, [T])).
> loop(T) ->
> receive
> switch ->
> io:format("switched~n"),
> loopie:loop(T);
> ANY ->
> io:format("received unexpected message >~p<.~n", [ANY]),
> loop(T)
> after T ->
> io:format("v1 ~p.~n", [module_info(attributes)]),
> loop(T)
> end.
>
> Here's a sample session:
>
> 1. initially loopie prints v1 and vsn every 5 seconds.
> 2. on the source "v1" is changed to "v2" and module is compiled andloaded
> 3. loopie is now printing v1 but with the new vsn
> 4. switch message is sent
> 5. loopie is now printing v2 and the new vsn
>
> $ erl
> Erlang R16B02 (erts-5.10.3) [source] [async-threads:10] [hipe] 
> [kernel-poll:false]
>
> Eshell V5.10.3 (abort with ^G)
> 1> l(loopie).
> {module,loopie}
> 2> loopie:start().
> true
> v1 [{vsn,[102112148784636006663528886950745967476]}].
> v1 [{vsn,[102112148784636006663528886950745967476]}].
> 3> l(loopie).
> {module,loopie}
> v1 [{vsn,[337256366447556252629333013282189761649]}].
> v1 [{vsn,[337256366447556252629333013282189761649]}].
> v1 [{vsn,[337256366447556252629333013282189761649]}].
> 4> loopie!switch.
> switched
> switch
> v2 [{vsn,[337256366447556252629333013282189761649]}].
> v2 [{vsn,[337256366447556252629333013282189761649]}].
> 5>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20131126/68f1bc4c/attachment.htm>


More information about the erlang-questions mailing list