0
+%% @author Yariv Sadan <yarivsblog@gmail.com> [http://yarivsblog.com]
0
+%% @copyright Yariv Sadan 2006-2007
0
-%% @title Smerl: Simple Metaprogramming for Erlang
0
-%% @doc <p>Smerl is an <a href="http://www.erlang.org">Erlang</a> library
0
-%% that simplifies the creation and manipulation of Erlang modules in
0
-%% <p>Smerl uses Erlang's capabilities for hot code swapping and
0
-%% abstract syntax tree parsing to do its magic. Smerl is inspired by
0
-%% the rdbms_codegen.erl module in the RDBMS application written by
0
-%% Ulf Wiger. RDBMS is part of <a href="http://jungerl.sf.net">Jungerl</a>.
0
-%% <p>Here's a quick example illustrating how to use Smerl:</p>
0
-%% M1 = smerl:new(foo),
0
-%% {ok, M2} = smerl:add_func(M1, "bar() -> 1 + 1."),
0
-%% foo:bar(), % returns 2``
0
-%% smerl:has_func(M2, bar, 0). % returns true
0
-%% <p>New functions can be expressed either as strings of Erlang code
0
-%% or as abstract forms. For more information, read the Abstract Format
0
-%% section in the ERTS User's guide
0
-%% (<a href="http://erlang.org/doc/doc-5.5/erts-5.5/doc/html/absform.html#4">link</a>).</p>
0
-%% <p>Using the abstract format, the 3rd line of the above example
0
-%% would be written as</p>
0
+%% @doc Smerl: Simple Metaprogramming for Erlang
0
+%% Smerl is an Erlang library
0
+%% that simplifies the creation and manipulation of Erlang modules in
0
+%% You don't need to know Smerl in order to use ErlyWeb; Smerl
0
+%% is included in ErlyWeb because ErlyWeb uses it internally.
0
+%% Smerl uses Erlang's capabilities for hot code swapping and
0
+%% abstract syntax tree transformations to do its magic. Smerl is inspired by
0
+%% the rdbms_codegen.erl module in the RDBMS application written by
0
+%% Ulf Wiger. RDBMS is part of Jungerl ([http://jungerl.sf.net]).
0
+%% Here's a quick example illustrating how to use Smerl:
0
+%% M1 = smerl:new(foo),
0
+%% {ok, M2} = smerl:add_func(M1, "bar() -> 1 + 1."),
0
+%% foo:bar(), % returns 2``
0
+%% smerl:has_func(M2, bar, 0). % returns true
0
+%% New functions can be expressed either as strings of Erlang code
0
+%% or as abstract forms. For more information, read the Abstract Format
0
+%% section in the ERTS User's guide
0
+%% ([http://erlang.org/doc/doc-5.5/erts-5.5/doc/html/absform.html#4]).
0
+%% Using the abstract format, the 3rd line of the above example
0
%% {ok,M2} = smerl:add_func(M1, {function,1,bar,0,
0
-%% [{op,1,'+',{integer,1,1},{integer,1,1}}]}]).``
0
+%% [{op,1,'+',{integer,1,1},{integer,1,1}}]}]).
0
%% <p>The abstact format may look more verbose in this example, but
0
-%% it's also more amenable to runtime manipulation.</p>
0
-%% <p>To manipulate or query an existing module rather than a new module,%%
0
-%% the first line could be rewritten such as:</p>
0
-%% ``smerl:for_module(mnesia),''
0
-%% <p><u>Detailed Description</u></p>
0
-%% <p>With Smerl, you can both create new modules and manipulate existing
0
-%% modules in runtime. You can also query whether a module has a given
0
-%% function by calling smerl:has_func. To start creating a new module, call
0
-%% smerl:new(ModuleName). To start modifying an existing module,
0
-%% call smerl:for_module(ModuleName). (The module be accessible
0
-%% with code:which and either have been compiled debug_info or its source
0
-%% file must in the same directory as the .beam file or in a ../src directory
0
-%% relative to the .beam file's ./ebin directory.) By calling smerl:for_file,
0
-%% you can create a new module from an Erlang source file.</p>
0
-%% <p>smerl:new, smerl:for_module and smerl:for_file return an
0
-%% MetaMod record for the module. To manipulate the module,
0
-%% use smerl:add_func and smerl:remove_func. Just remember not to
0
-%% add the same function name with the same arity twice as it will
0
-%% eventually result in a compilation error.</p>
0
-%% <p>When you're ready to compile your module, call smerl:compile,
0
-%% passing in the MetaMod record. If there are no errors,
0
-%% you can start using the new module.</p>
0
-%% <p>New capabilities (8/16/06):</p>
0
-%% <p>smerl:get_func, retrieves the abstract form for a given function,
0
-%% and smerl:replace_func, which does a smerl:remove_func followed by a
0
-%% <p>New capabilities (8/17/06):</p>
0
-%% <p>smerl:add_func and smerl:replace_func can now accept fun expressions
0
-%% as parameters. <b>This only works in the Erlang shell at the moment</b>.
0
-%% With fun expressions, you longer have to rely on source strings and
0
-%% forms to add behaviour to a module. Even closures are supported. Closure
0
-%% variables are expanded in the beginning the function. Example:
0
-%% {ok, C2} = smerl:add_func(C, bar, fun(B) -> A + B + 37 end),
0
-%% foo:bar(5). % returns 57``
0
-%% <p>Both smerl:add_func and smerl:replace_func support fun expressions.</p>
0
-%% @author Yariv Sadan <yarivvv@gmail.com> [http://yarivsblog.com]
0
-%% Copyright (c) 2006 Yariv Sadan
0
+%% it's also easier to manipulate in code.</p>
0
+%% Copyright (c) Yariv Sadan 2006-2007
0
%% Permission is hereby granted, free of charge, to any person
0
%% obtaining a copy of this software and associated documentation
0
-author("Yariv Sadan (yarivsblog@gmail.com, http://yarivsblog.com").
0
--define(L(Obj), io:format("LOG ~w ~p\n", [?LINE, Obj])).
0
--define(S(Obj), io:format("LOG ~w ~s\n", [?LINE, Obj])).
0
-%% @type meta_mod(). A tuple holding the abstract representation for module.
0
+-define(L(Obj), io:format("LOG ~s ~w ~p\n", [?FILE, ?LINE, Obj])).
0
+-define(S(Obj), io:format("LOG ~s ~w ~s\n", [?FILE, ?LINE, Obj])).
0
+-include_lib("kernel/include/file.hrl").
0
+%% @type meta_mod(). A data structure holding the abstract representation
0
%% @type func_form(). The abstract form for the function, as described
0
%% in the ERTS Users' manual.