好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Erlang 杂记 IV

Erlang 杂记 IV

休假回来,调整一下状态,先把Evernote里面一些比较零散的东西整理出来;过去一个月对于Erlang开发者还是有些惊喜的,比如《 Erlang / OTP 并发编程实战》终于出版了;比如<Building.Web.Applications.with.Erlang>也可以看到英文版了.下面第一条消息就是关于Erlang的另外一本好书:《Learn You Some Erlang》

 

Learn You Some Erlang 两则

  Erlang学习有一个非常棒的网站:  http://learnyousomeerlang.com/

  现在有两则关于它的消息:

   [1] 首先是这本书的Kindle版本, 能够在移动设备上阅读这本书,真的是非常方便;其实不仅仅是Kindle 其它支持mobi格式的都可以完美显示;甚至你可以使用源文件自己编译成为其它格式,比如epub;

    github地址: https://github.com/igstan/learn-you-some-erlang-kindle

  [2] 这个网站的出版物已经发行,其中文版由  @淘宝褚霸  翻译,可以预见到这又将成为中文资料的经典之作

    http://book.douban.com/subject/10822017/

 

 

string To Erlang Term 一个更为考虑更为完善的方法

下面是可以解决的:

 1 > E=fun(S) ->{ok,Scanned,_} =  erl_scan:  string(S),
{ok,Parsed} =   erl_parse:  parse_exprs(Scanned),
  erl_eval:  exprs(Parsed,[]) end.
#Fun<erl_eval.  6 . 111823515  >
  2 > E( "  {quit,tom}\n  " ++ "  .  "  ).
{value,{quit,tom},[]}
  3 >

如果是Pid的情况下会存在异常

** exception  error:   no match of right hand side value

                    {error,{  1 ,erl_parse,[ "  syntax error before:   " , "  '<'  " ]}} 

然后我在这里发现了一个更为完善的做法

https://github.com/erlang/otp/blob/master/lib/tv/src/tv_db_search.erl

string_to_term( Str  ) ->
    case catch   erl_scan: string( Str  ++  "  .   "  ) of
     {ok, ScannedStr, _No} ->
         case   erl_parse:  parse_term(ScannedStr) of
          {ok, Term} ->
              {ok, Term}  ;
            _Other ->
                 %% May be a PID, have to check this, since erl_scan
                 %% currently cannot handle this case...  :-(
              case catch list_to_pid(  Str  ) of
               Pid when is_pid(Pid) ->
                   {ok, Pid}  ;
                 _Error ->
                   case get(error_msg_mode) of
                    normal ->
                        {error, {not_a_term,   "  Please enter a valid term!  " }} ;
                      haiku ->
                        {error, {not_a_term, [  "  Aborted effort.  "  ,
                                       "  Reflect, repent and retype:  "  ,
                                       "  Enter valid term.  "  ]}}
                   end
              end
         end  ;
       _Error ->
         case get(error_msg_mode) of
          normal ->
              {error, {not_a_term,   "  Please enter a valid term!  " }} ;
            haiku ->
              {error, {not_a_term, [  "  Aborted effort.  "  ,
                             "  Reflect, repent and retype:  "  ,
                             "  Enter valid term.  "  ]}}
         end
    end.    

 

binary 替换一例 注意转义符的使用

这个没有什么特别的,只是要注意转义字符的使用

 85 >  binary: replace(<< "  c:\\windows\\abc\\c.txt  " >>,<< 92 >>,<< "  //  "  >>,[global]).
<<  "  c://windows//abc//c.txt  "  >>
  86 >  binary: replace(<< "  c:\\windows\\abc\\c.txt  " >>,<< "  \\  " >>,<< "  //  "  >>,[global]).
<<  "  c://windows//abc//c.txt  "  >>
  87 > 

 

ETS表中,根据表名获取ets表id,可用但是繁琐一点

 

 12 > Fun3=fun(Name)->[ ID|| ID <- ets: all(),Name== ets:  info(ID,name)] end.

#Fun<erl_eval.  6 . 111823515  >

  13  > Fun3(inet_db).

[inet_db]

  14  > Fun3(code).

  "  \r  " 

 15  > Fun3(code_names).

[  4110  ]

  16 >

 

erlang模块下面有一个非常好用的解包方法

下面的解析可以使用string或binary模块自行解析,其实使用erlang:packet直接可以得到结果

Eshell V5. 9 . 1    (abort with ^G)
  1 >  erlang: decode_packet(http,<< "  GET http://www.google.com HTTP/1.1\n  "  >>,[]).
{ok,{http_request,  '  GET  '  ,
                  {absoluteURI,http,  "  www.google.com  " ,undefined, "  /  "  },
                  {  1 , 1  }},
    <<>>}
  2 >  erlang: decode_packet( 1 ,<< 3 , "  abcd  "  >>,[]).
{ok,<<  "  abc  " >>,<< "  d  "  >>}
  3 >   erlang: decode_packet( 1 ,<< 5 , "  abcd  "  >>,[]).
{more,  6  }
  4  >

  

erlang:decode_packet的官方文档地址: http://www.erlang.org/doc/man/erlang.html

 

Erlang字符串处理的两个开源项目

[1]  http://code.google.com/p/starling/

Unicode strings for Erlang

Starling is a Unicode string library for Erlang based on  ICU , the best implementation of Unicode.

It's implemented as a C pipe driver packaged as an OTP application. Strings are stored as binaries internally. Compatibility layer with the standard  string  module is provided.

A mailing list has been set up at  http://groups.google.com/group/starling-discuss .

The project homepage is at  http://12monkeys.co.uk/starling .

[2] https://github.com/beerriot/icu4e  icu4e: Erlang NIF wrappers around icu4c

 

More Sharing Services Share | Share on facebook Share on myspace Share on google Share on twitter

 


 

分类:  Erlang

作者: Leo_wl

    

出处: http://www.cnblogs.com/Leo_wl/

    

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

版权信息

查看更多关于Erlang 杂记 IV的详细内容...

  阅读:58次