免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1969 | 回复: 1
打印 上一主题 下一主题

[书评] why-i-often-implement-things-from [复制链接]

论坛徽章:
27
水瓶座
日期:2014-08-22 21:06:34程序设计版块每日发帖之星
日期:2015-11-25 06:20:0015-16赛季CBA联赛之新疆
日期:2015-12-19 19:05:48IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:3315-16赛季CBA联赛之上海
日期:2016-04-15 19:51:31程序设计版块每日发帖之星
日期:2016-04-17 06:23:29程序设计版块每日发帖之星
日期:2016-04-23 06:20:00程序设计版块每日发帖之星
日期:2016-05-26 06:20:00每日论坛发贴之星
日期:2016-05-26 06:20:0015-16赛季CBA联赛之辽宁
日期:2017-02-16 23:59:47
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-09-03 01:15 |只看该作者 |倒序浏览
  1. Once upon a time there was an Erlang programmer who needed an FTP server running on one of the hosts in a private network. In fact he didn't need an FTP server, he just needed to transfer files between a central server and his client machine, but he thought that he needed an FTP server to do this.

  2. He searched for FTP servers, and indeed found several of them. They had to be free of course, because even though the organisation he worked for had loads of money the administrative procedures for buying such a product were considerable. The program also had to have the right form of license, so that the legal department would be happy.

  3. He downloaded several such servers, some of them wouldn't compile, and even if they did compile they had to be correctly configured in order to work, and this was not easy.

  4. Suddenly our programmer was stuck by the thought that he might be able to write an FTP server himself and that writing the FTP server might be quicker than finding and installing an FTP server that somebody else had written.

  5. "What do I want to do?" he asked himself.

  6. "List files on a remote directory, copy files, between a local and remote machine, etc"

  7. Then he remembered that the project he was working on used distributed Erlang.

  8. "This must be easy," he thought. He was right.

  9. So ... on the server machine he give this command:

  10. >$erl -name server -cookie ASDRTFERT
  11. 1> node().
  12. 'server@host1.somenet.com'
  13. [See this link for a description of what these commands do]. Then he went to another machine and typed this:
  14. >$erl -name client1 -cookie ASDRTFERT
  15. 1> node().
  16. 'client1@host23.somenet.com'
  17. Now he had started two erlang nodes. Now the nice thing about distributed Erlang is that you can easily run code on any of the nodes, so to check that he could access the server node from the client node, the programmer typed this:

  18. 2> rpc:call(server@host1.somenet.com',
  19. erlang, node, []).
  20. 'server@host1.somenet.com'
  21. Which is what would have happened if the command had been issued on the first machine. Now our programmer knew that he could evaluate any function on the remote machine, just as if it had been on the local machine. The correspondence is as follows:

  22. If the local command was:
  23. > file:get_cwd()
  24. Then to perform this on the server all he had to do was call:
  25. > rpc:call(server@host1.somenet.com',file, get_cwd, []).
  26. So to list files on the remote machine, he did this:
  27. 1> rpc:call(server@host1.somenet.com',
  28.      file, list_dir, ["."]).
  29. {ok, ["Makefile",
  30.   "readme",
  31. ....
  32. Then he decided that he wanted to copy the Makefile to his local machine so he wrote this:
  33. 2> {ok, Bin} = rpc:call(server@host1.smenet.com',
  34.                 file, read_file, ["Makefile"]).
  35. <<".SUFFIXES: .erl .beam .yrl" .....>>
  36. 3> file:write_file("Makefile", [Bin]).
  37. ok
  38. At this point all that typing in the shell became tedious, so he fired up emacs and wrote this:
  39. -module(myftp).
  40. -export([get_file/1]).

  41. get_file(F) ->
  42. {ok, B} = rpc:call(server@host1.smenet.com',
  43.                   file, read_file, [F]),
  44. file:write_file(F ++ ".copy", [B]).
  45. Then he compiled and tested his program:
  46. 4> c(myftp).
  47. {ok,myftp}
  48. 5> myftp:get_file("Makefile").
  49. ok
  50. He then added a few bells and whistles and was done.

  51. Moral

  52. If you have the right tools it's often quicker to implement something from scratch than going to all the trouble of downloading compiling and installing something that somebody else has written.

  53. This is a true story, but I've just written this code. Writing this code and the blog entry took about the same time as it would to find and install an FTP server on my machine.
复制代码

论坛徽章:
27
水瓶座
日期:2014-08-22 21:06:34程序设计版块每日发帖之星
日期:2015-11-25 06:20:0015-16赛季CBA联赛之新疆
日期:2015-12-19 19:05:48IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:3315-16赛季CBA联赛之上海
日期:2016-04-15 19:51:31程序设计版块每日发帖之星
日期:2016-04-17 06:23:29程序设计版块每日发帖之星
日期:2016-04-23 06:20:00程序设计版块每日发帖之星
日期:2016-05-26 06:20:00每日论坛发贴之星
日期:2016-05-26 06:20:0015-16赛季CBA联赛之辽宁
日期:2017-02-16 23:59:47
2 [报告]
发表于 2014-09-03 01:16 |只看该作者
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP