Sorry yes one or 2 bugs because I just typed it in the email, <br>this version works<br><font style="font-family: comic sans ms,sans-serif;" size="1"><br>log_message(File)-><br>
receive<br>
{open,Path} -><br>
case file:open(Path, [append]) of<br>
{ok,F} -><br>
log_message(F);<br>
{error,Reason} -><br>
io:format( "~s~n",[Reason])<br>
end;<br>
{data,Data} -> <br>
case File of<br>
not_open -><br>
exit(oops_file_not_open);<br>
_Else -><br>
io:format(File,"~s~n",[Data]),<br>
log_message(File)<br>
end<br>
end<br>
<br>1>Pid = spawn(fun() -> yourmodule:log_message(not_open) end,<br>
2>Pid ! {open, "/path/to/file.txt"}.<br>
3>Pid ! {data, "some string"}.</font><br><br><div class="gmail_quote">2009/4/2 Gamoto <span dir="ltr"><gamoto@bluewin.ch></span><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div>
<div><font size="1">Are you sure this code is correct ?</font></div>
<div><font size="1">lines 1 and 16: log_message(FileD) </font></div>
<div><font size="1">line 4: open(File, ...)</font></div>
<div><font size="1"></font> </div>
<div><font size="1">But I am newbie ... then correct me if I am wrong ...</font></div><div><div></div><div class="h5">
<div><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">log_message(File)-></span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> receive</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> {open,File} ->
<br> case file:open("File", [append]) of</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
{ok,FileD} -> <br>
log_message(FileD);</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
{error,Reason} -> <br>
io:format( "~s~n",[Reason])</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> end;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> {data,Data}
-> <br> case File of
<br> not_open ->
<br>
exit(oops_file_not_open);<br>
Else -> <br>
io:format(File,"~s~n",[Data]),</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
log_message(File)</span><br>
end<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> end.</span><br><br>is
closer to what you are wanting, variable only have scope<br>within a function
call, this isnt a closure, to keep a variable<br>in scope you pass it into the
next call of the function.<br><br></div>
<div class="gmail_quote">2009/4/2 Robert Virding <span dir="ltr"><<a href="mailto:rvirding@gmail.com" target="_blank">rvirding@gmail.com</a>></span><br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="gmail_quote">2009/4/2 Gamoto <span dir="ltr"><gamoto@bluewin.ch></span>
<div><br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">What
is wrong in this function ? I receive the following error: variable 'S' is
unbound !<br>I also receive two warnings: File is unused, S is
unused.<br>This process can receive two messages: one file to open and one
message to write<br><br>Pid ! {open,"filetoopen.txt"}<br><br>Pid ! {data,
Message}<br><br>log_message()-><br> receive<br>
{open,File} -> case
file:open("File", [append]) of<br>
{ok,S} -> log_message();<br>
{error,Reason} -> io:format(
"~s~n",[Reason])<br>
end;<br>
{data,Data} -> io:format(S,"~s~n",[Data]),<br>
log_message()<br> end.</blockquote></div>
<div><br>- The variable File is defined in the message {file,File} you recieve
but is never used, you open the file "File".<br><br>- The variable S is
defined when you receive the {open,File} message, but you wish to use it when
you receive the {data,Data} message where it is not
defined.<br><br>Robert<br><br></div></div><br>_______________________________________________<br>erlang-questions
mailing list<br><a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br><a href="http://www.erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://www.erlang.org/mailman/listinfo/erlang-questions</a><br>
</blockquote></div><br></div></div></div>
</blockquote></div><br>