这里集中记录我的Erlang编程过程中零散发现的小的技巧以及代码之类的
防止被不良网站抓取,先不写具体内容,过几天再来补上
防止被不良网站抓取,先不写具体内容,过几天再来补上
#### 方便debug的macro定义 #### 2010-11-06
#### 一个函数加载所有的重新编译的代码 #### 2010-10-23
经常,会改变一个代码,逐个l(module)的话,很麻烦,一个lm()全搞定
lm() ->
[c:l(M) || M <- mm()].
mm() ->
modified_modules().
modified_modules() ->
[M || {M, _} <- code:all_loaded(),
module_modified(M) == true].
module_modified(Module) ->
case code:is_loaded(Module) of
{file, preloaded} ->
false;
{file, Path} ->
CompileOpts =
proplists:get_value(compile, Module:module_info()),
CompileTime = proplists:get_value(time, CompileOpts),
Src = proplists:get_value(source, CompileOpts),
module_modified(Path, CompileTime, Src);
_ ->
false
end.
module_modified(Path, PrevCompileTime, PrevSrc) ->
case find_module_file(Path) of
false ->
false;
ModPath ->
case beam_lib:chunks(ModPath, ["CInf"]) of
{ok, {_, [{_, CB}]}} ->
CompileOpts = binary_to_term(CB),
CompileTime = proplists:get_value(time,
CompileOpts),
Src = proplists:get_value(source, CompileOpts),
not (CompileTime == PrevCompileTime) and
(Src == PrevSrc);
_ ->
false
end
end.
find_module_file(Path) ->
case file:read_file_info(Path) of
{ok, _} ->
Path;
_ ->
%% may be the path was changed
case code:where_is_file(filename:basename(Path)) of
non_existing ->
false;
NewPath ->
NewPath
end
end.
#### 方便debug的macro定义 #### 2010-11-06
-define(x(X), io:format("~p~p: ~s = ~p~n", [?MODULE, ?LINE, ??X, X])).
test() ->
VeryLongVariableName = foo(),
?x(VeryLongVariableName).