[erlang-questions] ERTS; code-server; dynamically loading of code

Ulf Wiger (TN/EAB) <>
Mon Sep 18 16:32:12 CEST 2006


 
Of course, toying with dynamic code loading is great
fun. In a current project, I do dynamic code loading
from mnesia on demand. Rather than messing with the
error handler, I do something like this:

try_module(Who, UserStr, Mod, IsOwner) ->
    try list_to_existing_atom(Mod) of
        ActualName ->
            case erlang:module_loaded(ActualName) of
                true ->
                    ...;
                false ->
                    try_load(Who, UserStr, Mod)
            end
    catch
        ...
    end.

try_load(Who, UserStr, Mod) ->
    ...,
    case mnesia:read({?CODE_TABLE, list_to_binary(Mod)}) of
        [] ->
            false;
        [#tups{value = #mod{bin = Bin}}] ->
            ...,
            case code:load_binary(ActualName, Fname, Bin) of
                {module, _} ->
                    {true, ActualName};
                ...
            end
    end.

Prototype code, mind you, but it seems to work
reasonably well. Points of interest are:

- list_to_existing_atom/1 to avoid dynamically 
  creating unknown atoms
- erlang:module_loaded/1
- code:load_binary/3

BR,
Ulf W


> -----Original Message-----
> From:  
> [mailto:] On Behalf Of 
> Matthias Kretschmer
> Sent: den 18 september 2006 15:53
> To: 
> Subject: Re: [erlang-questions] ERTS; code-server;dynamically 
> loading of code
> 
> Hello,
> 
> thank you two for the information.
> 
> On Mon, Sep 18, 2006 at 11:05:02AM +0200, Ulf Wiger (TN/EAB) wrote:
> > I second Lennart's opinion. Don't mess with the code 
> loading unless you've become One with the Erlang environment.
> > 
> > [...]
> 
> Don't worry, I at least think I know what I am doing and I am 
> just toying around and experimenting a bit. So the next plane 
> you use wouldn't crash because of me and toying around with 
> code loading. Thank god! :-)
> 
> Just had in mind doing some more stuff than just loading a 
> module. I know, that modules are dynamically loaded. Anyway 
> just toying around with the Erlang runtime.
> 
> --
> Cheers,
> Matthias Kretschmer
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
> 




More information about the erlang-questions mailing list