[erlang-questions] http:request

Kostis Sagonas kostis@REDACTED
Fri Apr 22 09:49:14 CEST 2011


Carlo Bertoldi wrote:
> Il 21/04/2011 17:10, Carlo Bertoldi ha scritto:
>> Good afternoon,
>>  I have a problem with http:request.
>>
> <snip>
>>
>> Result = http:request(get, {CompleteUrl, [{"User-Agent", "Erl-bot"}]}, 
>> [], []),
>> io:format("Result = ~p~n", [Result]).
>>
>> Occasionally I don't see Result printed, but a nice error:
>> ** Reason for termination ==
>> ** {{badmatch,{16,6,18}},
> 
> Hello, I discovered the cause of the badmatch: it was due to the debug 
> macro I've defined.
> Here it is:
> 
> -define(DBG(Str, Args),
>   {Year, Month, Day} = date(),
>   {Hour, Min, Sec} = time(),
>   io:format("~2.10.0B/~2.10.0B/~4B ~2.10.0B:~2.10.0B:~2.10.0B [~p:~p] - 
> ", [Day, Month,
>        Year, Hour, Min, Sec, ?MODULE, ?LINE]), io:format(Str, Args)).
> 
> Can someone explain me what I did wrong here? Why should I obtain a 
> badmatch on {Hour, Min, Sec} = time() ?

A typical reason for this is if you used the macro in some context where 
variables named Hour, Min, and Sec already existed.  This is called name 
capture and is a typical problem of macros. You should carefully examine 
all places in the code where this macro is used.  Alternatively, you can 
try to minimize the risk of name capture by changing the macro to e.g.:

-define(DBG(Str, Args),
         {Year__, Month__, Day__} = date(),
         {Hour__, Min__, Sec__} = time(),
    io:format("~2.10.0B/~2.10.0B/~4B ~2.10.0B:~2.10.0B:~2.10.0B [~p:~p] 
-  ", [Day__, Month__, Year__, Hour__, Min__, Sec__, ?MODULE, ?LINE]), 
io:format(Str, Args)).


Kostis



More information about the erlang-questions mailing list