gen_tcp:send failing silently
Samuel Tardieu
sam@REDACTED
Mon Feb 23 04:09:22 CET 2004
> Things have evolved since last message. I've managed to put the
> little bot online.
Great!
> Yeah, that's what I thought too, but the most elegant things with
> pattern matching is:
> case Data of [80,73,78,71] -> ...
> and that gets ugly when the string is longer. I'm probably missing
> something about pattern-matching.
Implement a full fledged parser for the IRC protocol then. Here is
mine:
%%--------------------------------------------------------------------
%% Function: parse/1
%% Description: Return a list of arguments in an IRC command. The
%% answer is a tuple {Sender, Command} where Sender can
%% be false if no server has been detected.
%%--------------------------------------------------------------------
parse ([$: | Line]) ->
[Sender | Rest] = string:tokens (Line, " "),
{false, Command} = parse (join (Rest, " ")),
{Sender, Command};
parse (Line) ->
[NoNewLine | NewLine] = string:tokens (Line, "\r\n"),
[Before | Rest] = string:tokens (NoNewLine, ":"),
End = case join (Rest, " ") of
[] -> [];
X -> [X]
end,
{false, string:tokens (Before, " ") ++ End}.
join ([], _) -> "";
join ([X], _) -> X;
join ([X | Xs], C) -> X ++ C ++ join (Xs, C).
For example, if you feed parse with "PING :1234567", it will return
{false, ["PING", "1234567"]}.
> I'm slightly bothered by this behavior. An error like a mistyped
> function like "substring" should for raise some kind of warning on
> the console. Perhaps I'm too used to stricter language where such
> an error is treated as a catastrophe: "Compiler error: OMG! THAT
> FUNCTION DOESN'T EXIST! RUN FOR YOUR LIFE"...
> Any advices here? I'm a simple comp. sci. student that usually
> prefers Java for homeworks.
You should use Erlang cross-referencer to look for undefined external
functions (try "erl -man xref").
Sam
--
Samuel Tardieu -- sam@REDACTED -- http://www.rfc1149.net/sam
More information about the erlang-questions
mailing list