[erlang-questions] how to patch a standard module

James Churchman <>
Thu Jan 13 22:39:47 CET 2011


Lots of dif ways i can think of, but generally it's a bit of a bad idea as it goes down the multiple inheritance route of OO 

-include(…..).

Usually works if : 
1) your module is still named the same as the original
2) you are adding a function with a unique name/arity combination, and not changing, removing etc… any others
3) you set the include directory to the file, when you compile. 

So in your original module (that you name "httpd_request.erl" and have no module declaration in):

-include("/usr/local/lib/erlang/lib/inets-5.5.1/src/http_server/httpd_request.erl").
%% this is dependant on your instalation, and i think include_lib wont work in this case, as it will try to include its self
options(My_Input)->my_output.

Then compile code with :

compile:file("/Users/james/Desktop/httpd_request.erl",[{i,"/usr/local/lib/erlang/lib/inets-5.5.1/src/http_server/"}]).
%% again dependant on your erlang installation and file location

Modifying is more tricky and requires getting the AST source representation of the two modules ( probably in a parse transform of the 1st), removing one of the module names (as if you have two, it wont compile), then sorting all the functions to the bottom of the list then returning that to the parse transform. Sorting is required as erlang wont compile a file that has functions before attributes in the source / AST.
This can be packaged up into a general thing however. Meck could also be used, which probably does a similar thing, but I think that it's only designed for testing and will revert to the old module on errors etc..


Is there defiantly no way to achieve what you want without extending the original code?? i would bet that there is!

James


On 13 Jan 2011, at 15:52, Gijsbert wrote:

> Hi,
> 
> I'd like to extend httpd_request.erl to handle the OPTIONS method.
> I've managed to do this by copying httpd_request.erl into my project
> and making sure it overrides the standard version.
> My question: is it possible to redefine a function in a standard
> module at runtime? Then I could define my own validate(Method, Uri,
> Version) function (just a few lines) and keep using the standard
> httdp_request.erl.
> 
> Regards,
> Gijsbert
> 
> ________________________________________________________________
> erlang-questions (at) erlang.org mailing list.
> See http://www.erlang.org/faq.html
> To unsubscribe; mailto:
> 



More information about the erlang-questions mailing list