[erlang-questions] Basic Erlang case question
Lev Walkin
vlm@REDACTED
Thu Jul 10 11:11:57 CEST 2008
Alexander Lamb wrote:
> Hello List,
>
> Here is a very simple code to search if a supplied password is correct
> or not:
>
> F1 = fun() -> mnesia:match_object(systems,
> #system_info{full_attribute = {System_Name,admin_password}, _ = '_'},
> read) end,
> case Password of
> [] -> MD5_Password = [];
> _Any -> MD5_Password = erlang:md5(Password)
> end,
> case mnesia:transaction(F1) of
> {atomic, []} -> {error, unknown_system};
> {atomic, [#system_info{data = MD5_Password}]} -> {ok, password};
> {atomic, _} -> {error, bad_password};
> {aborted, Reason} -> {error, Reason}
> end
>
>
>
> Basically, if supplied Password is empty, the MD5_Password must be
> empty. If not, I encrypt the Password to compare it with stored version.
>
> My problem is that it compiles with a warning:
>
> ./cs_systems.erl:173: Warning: variable 'MD5_Password' exported from
> 'case' (line 167)
>
> Why? Obviously I am trying to match the password in the record I found
> with the password I supply.
This Erlang "feature" of leaking identifiers out of "case" is
something to be avoided, like goto's in imperative languages.
Instead of doing
case Foo of
Bar -> Password = f1(Bar);
Baz -> Password = f2(Baz)
end
do this:
Password = case Foo of
Bar -> f1(Bar);
Baz -> f2(Baz)
end
In your case,
HashedPassword = case Password of
[] -> []
Value -> erlang:md5(Value)
end
--
vlm
More information about the erlang-questions
mailing list