erlang -import是如何工作的?

时间:2014-02-07 20:47:49

标签: erlang

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

  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看起来像

-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看起来像:

-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模块发现算法的文档/文章的任何链接。

1 个答案:

答案 0 :(得分:4)

erlang中的用法不是导入任何函数。由于在编译期间没有链接阶段,并且由于调用在运行时被解决,因此它是无用的。顺便说一下导入不会导入任何东西,只是简单地缩短代码的编写。导入指令-import(mochiweb_html1, [parse/1]).将简单地在编译之前用mochiweb_html1:parse(Foo)替换对parse(Foo)的任何调用,不再做任何事情,不检查任何内容。编译代码时函数可能不存在,这不是问题。

所以一切都是运行时代码路径和库路径的问题。您可以查看优秀网站learnyousomeerlang,特别是这些章节:modules,如果您已经熟悉OTP library-applications