<div dir="ltr">This is what I have been using successfully for a short while now. Hope this helps.<br><br><span style="font-family: courier new,monospace;">%%</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">%% Compile from string hack by Mats Cronqvist (Thank You!!)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">%% NOTE: Does not work if there are macros in Text!</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">%%</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">compile_and_load(Text) -></span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> try</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> Forms = ?MODULE:scan_and_parse(Text, 1, []),</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> {ok, Mod, Bin} = compile:forms(Forms),</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> {module, _M} = code:load_binary(Mod, "generated", Bin),</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> io:format(</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> "~p:compile_and_load replaced module ~p with [~s]~n",</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> [?MODULE, Mod, Text]</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> ),</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> code:purge(Mod),</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> ok</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> catch</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> Err:Reason -></span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> io:format(</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> "compile_and_load failed, err:reason {~p:~p}~n~p~n",</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> [Err, Reason, Text]</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> ),</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> {Err, Reason}</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> end.</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">scan_and_parse([], _Line, Forms) -></span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> lists:reverse(Forms);</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">scan_and_parse(Text, Line, Forms) -></span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> {done, {ok, Toks, NLine}, Cont} = erl_scan:tokens([], Text, Line),</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> {ok, Form} = erl_parse:parse_form(Toks),</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> ?MODULE:scan_and_parse(Cont, NLine, [Form|Forms]).</span><br><br><br><br><div class="gmail_quote">2008/9/27 Robert Virding <span dir="ltr"><<a href="mailto:rvirding@gmail.com">rvirding@gmail.com</a>></span><br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div dir="ltr">I am generating compiled erlang code on the fly without saving using files (using LFE) and need to load the compiled binary into the system. Now, I know how to do myself "by hand" but I would prefer to go through the normal code loader to keep it all clean.<br>
<br>There is a call code:load_binary(Module, FileName, Binary) which seems to do what I want, BUT there is this argument FileName. Now I don't intend to save the module in a file unless I really have to as it seems like a bit of unnecessary work to save it in a file just so I can load it. What is the significance of the FileName argument and does it matter what I set it to? Can I just call Module.beam and be done with?<br>
<br>Robert<br><br></div>
<br>_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://www.erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://www.erlang.org/mailman/listinfo/erlang-questions</a><br></blockquote></div><br></div>