[erlang-questions] Copy function from one Module to another

Geoff Cant nem@REDACTED
Tue Aug 14 20:01:25 CEST 2012


You won't be able to do exactly what you have asked for as functions only exist inside modules. Even funs are just context plus a reference to code in a module. You cannot run a fun on a node that doesn't have the module it references. 

While I'm sure that Fred's idea is the best (you'd have to be sorely bandwidth constrained before other trade offs make sense) the middle ground would be to compile a new module with just the function + deps you want to migrate and send that to the remote node. You would probably need debug_info in the original module for this to work. 

In your position I would use Fred's scheme. The 3-liner beats the complicated recompilation solution 99 times out of 100. 

-Geoff

On 14/08/2012, at 9:20, Tyron Zerafa <tyron.zerafa@REDACTED> wrote:

> I want to implement code migration where a user just will be able to call migrate(RemoteNode, Fun) and that local fun will end up on the RemoteNode. 
> Thus this needs to be generic. I cannot assume that:
> 1) All functions within the module are going to be used - transferring code which will not be used will result in useless bandwidth costs.
> 2) All functions required are within a single module 
> 
> I was going to perform a static analysis of the function to be migrated and transfer only the functions it depends on. 
> 
> On Tue, Aug 14, 2012 at 6:09 PM, Fred Hebert <mononcqc@REDACTED> wrote:
> Why do you want to do this, instead of, for example, just sending the module to the other node?
> 
> {Mod, Bin, File} = code:get_object_code(Mod),
> rpc:call(Node, code, load_binary, [Mod, File, Bin]).
> 
> or alternatively use rpc:multicall(Nodes) for many nodes and doing the same.
> 
> 
> On 12-08-14 11:15 AM, Tyron Zerafa wrote:
>> 
>> Hey Jesse
>>     I need something stronger than just import. I need to put a bunch of functions from different modules into a single one and transfer this to a remote node. Then I want to be able to use these functions from the remote node. 
>> If I simply use import, I will not be able to use them on the remote node without explicitly transferring all the modules to which the functions belong to.
>> 
>> 
>> On Tue, Aug 14, 2012 at 5:10 PM, Jesse Gumm <gumm@REDACTED> wrote:
>> That sounds like you're looking for
>> 
>> -import(Module, Functions).
>> 
>> http://www.erlang.org/doc/reference_manual/modules.html
>> 
>> --
>> Jesse Gumm
>> Owner, Sigma Star Systems
>> 414.940.4866 || sigma-star.com || @jessegumm
>> 
>> On Aug 14, 2012 9:56 AM, "Tyron Zerafa" <tyron.zerafa@REDACTED> wrote:
>> Hey all,
>>     Is there any mechanism in Erlang which I can use to copy functions from one module to another? So, if I have module A containing functions a, b and c, is there any way in which I can copy a to module B?
>> 
>> Thanks 
>> Tyron
>> 
>> 
>> _______________________________________________
>> erlang-questions mailing list
>> erlang-questions@REDACTED
>> http://erlang.org/mailman/listinfo/erlang-questions
>> 
>> 
>> 
>> 
>> -- 
>> Best Regards,
>> Tyron Zerafa
>> 
>> 
>> 
>> _______________________________________________
>> erlang-questions mailing list
>> erlang-questions@REDACTED
>> http://erlang.org/mailman/listinfo/erlang-questions
> 
> 
> 
> 
> -- 
> Best Regards,
> Tyron Zerafa
> 
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20120814/41df01f1/attachment.htm>


More information about the erlang-questions mailing list