erlang -import如何工作? [英] How does erlang -import work?

查看:83
本文介绍了erlang -import如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用mochiweb编写一些应用程序.应用树:

I'm trying to write some app using mochiweb. App tree:

  tree
    .
    ├── aniaggregator2.iml
    ├── deps
    │   └── mochiweb
    │       ├── CHANGES.md
    │       ├── doc
    │       ├── ebin
    │       │   ├── ....
    │       │   ├── mochiweb_html.beam
    │       │   ├── ....
    │       ├── examples
    │       │   ├── ......
    │       ├── include
    │       │   └── ......
    │       ├── LICENSE
    │       ├── Makefile
    │       ├── README
    │       ├── rebar
    │       ├── rebar.config
    │       ├── scripts
    │       │   ├── entities.erl
    │       │   └── new_mochiweb.erl
    │       ├── src
    │       │   ├── .....
    │       │   ├── mochiweb_html.erl
    │       │   ├── .....
    │       ├── support
    │       │   ├── .....
    │       └── test
    │           .....
    ├── ebin
    │   ├── aniaggregator.app
    │   ├── aniaggregator_app.beam
    │   ├── aniaggregator.beam
    │   ├── aniaggregator_deps.beam
    │   ├── aniaggregator_sup.beam
    │   └── aniaggregator_web.beam
    ├── LICENSE
    ├── Makefile
    ├── priv
    │   └── www
    │       └── index.html
    ├── README.md
    ├── rebar
    ├── rebar.config
    ├── src
    │   ├── .....
    │   └── parsers
    │       ├── default_parser.beam
    │       ├── default_parser.erl
    │       ├── default_parser_tests.beam
    │       ├── default_parser_tests.erl
    │       └── erl_crash.dump
    └── start-dev.sh

default_parser.erl看起来像

default_parser.erl looks like

-module(default_parser).
-author("psih").
-import(mochiweb_html, [parse/1]).

%% API
-export([parse_from_url/1, fetch_names_from_animelist/0]).

parse_from_url(Url) ->
  {ok, {Status, Headers, Body}} = httpc:request(Url),
  Status.


fetch_names_from_smth() ->
  Url = "some_url",
  {ok, {Status, Headers, Body}} = httpc:request(Url),
  {String, Attributes, Other} = parse(Body),
  Attributes.

default_parser_tests.erl如下:

default_parser_tests.erl looks like:

-module(default_parser_tests).
-include_lib("eunit/include/eunit.hrl").

start() ->
  application:start(inets),
  ok.

stop(_) ->
  application:stop(inets),
  ok.

do_some_testing(_) ->
  erlang:display(default_parser:fetch_names_from_smth()),
  [?_assert(true =:= true)].


do_some_test_() ->
  {"Performs some default parsing stuff!",
    {setup,
      fun start/0,
      fun stop/1,
      fun do_some_testing/1
    }
  }.

的输出!erlc default_parser.erl&&erlc default_parser_tests.erl&&erl -pa -noshell -eval"eunit:test(default_parser)" -s init stop 类似于:

*** instantiation of subtests failed ***
::error:undef
  in function mochiweb_html1:parse/1
    called as parse(....)
  in call from default_parser:fetch_names_from_smth/0
  in call from default_parser_tests:do_some_testing/1


=======================================================
  Failed: 0.  Skipped: 0.  Passed: 0.
One or more tests were cancelled.

所以,我的问题是-如何将模块从mochiweb *导入到我的特定应用程序?我也将很感谢有关erlang模块发现算法的文档/文章的任何链接.

So, my question is - how can I import modules from mochiweb* to my particular app? also I will appreciate any links to documentation/articles about erlang's module's discovering algorithms.

推荐答案

erlang中的用法是不导入任何函数.由于在编译过程中没有链接阶段,并且在运行时解决了调用,因此没有用.顺便说一句,import不会导入任何内容,而只是缩短了代码编写时间.导入指令 -import(mochiweb_html1,[parse/1]).只会在编译之前用mochiweb_html1:parse(Foo)替换对parse(Foo)的任何调用,不执行任何操作,不检查任何内容.该功能在您编译代码时可能不存在,这不是问题.

The usage in erlang is not to import any function. As there is no link phase during compilation, and as the call is solved at run time it is useless. By the way import does not import anything but simply shorten the writing of the code. The import directive -import(mochiweb_html1, [parse/1]). will simply replace any call to parse(Foo) by mochiweb_html1:parse(Foo) before compilation , nothing more is done, nothing is checked. the function may not exist at the time you compile the code, it is not an issue.

因此,一切都与运行时的代码路径和库路径有关.您可以浏览优秀的网站 learnyousomeerlang ,尤其是以下章节:

So everything is a matter of code path and library path at run time. You could have a look to the excellent site learnyousomeerlang particularly to these chapter: modules and if you have already a good knowledge of OTP library-applications

这篇关于erlang -import如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆