[erlang-questions] variable unbound
Dale Harvey
harveyd@REDACTED
Thu Apr 2 16:21:19 CEST 2009
Sorry yes one or 2 bugs because I just typed it in the email,
this version works
log_message(File)->
receive
{open,Path} ->
case file:open(Path, [append]) of
{ok,F} ->
log_message(F);
{error,Reason} ->
io:format( "~s~n",[Reason])
end;
{data,Data} ->
case File of
not_open ->
exit(oops_file_not_open);
_Else ->
io:format(File,"~s~n",[Data]),
log_message(File)
end
end
1>Pid = spawn(fun() -> yourmodule:log_message(not_open) end,
2>Pid ! {open, "/path/to/file.txt"}.
3>Pid ! {data, "some string"}.
2009/4/2 Gamoto <gamoto@REDACTED>
> Are you sure this code is correct ?
> lines 1 and 16: log_message(FileD)
> line 4: open(File, ...)
>
> But I am newbie ... then correct me if I am wrong ...
>
> log_message(File)->
> receive
> {open,File} ->
> case file:open("File", [append]) of
> {ok,FileD} ->
> log_message(FileD);
> {error,Reason} ->
> io:format( "~s~n",[Reason])
> end;
> {data,Data} ->
> case File of
> not_open ->
> exit(oops_file_not_open);
> Else ->
> io:format(File,"~s~n",[Data]),
> log_message(File)
> end
> end.
>
> is closer to what you are wanting, variable only have scope
> within a function call, this isnt a closure, to keep a variable
> in scope you pass it into the next call of the function.
>
> 2009/4/2 Robert Virding <rvirding@REDACTED>
>
>> 2009/4/2 Gamoto <gamoto@REDACTED>
>>
>>> What is wrong in this function ? I receive the following error: variable
>>> 'S' is unbound !
>>> I also receive two warnings: File is unused, S is unused.
>>> This process can receive two messages: one file to open and one message
>>> to write
>>>
>>> Pid ! {open,"filetoopen.txt"}
>>>
>>> Pid ! {data, Message}
>>>
>>> log_message()->
>>> receive
>>> {open,File} -> case file:open("File", [append]) of
>>> {ok,S} ->
>>> log_message();
>>> {error,Reason} -> io:format(
>>> "~s~n",[Reason])
>>> end;
>>> {data,Data} -> io:format(S,"~s~n",[Data]),
>>> log_message()
>>> end.
>>
>>
>> - The variable File is defined in the message {file,File} you recieve but
>> is never used, you open the file "File".
>>
>> - 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.
>>
>> Robert
>>
>>
>> _______________________________________________
>> erlang-questions mailing list
>> erlang-questions@REDACTED
>> http://www.erlang.org/mailman/listinfo/erlang-questions
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20090402/f6ed3d82/attachment.htm>
More information about the erlang-questions
mailing list