How does erlang -import work?

时间秒杀一切 提交于 2021-01-28 07:06:33

问题


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 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 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
    }
  }.

output of !erlc default_parser.erl && erlc default_parser_tests.erl && erl -pa -noshell -eval "eunit:test(default_parser)" -s init stop is something like:

*** 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.

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.


回答1:


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.

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



来源:https://stackoverflow.com/questions/21637453/how-does-erlang-import-work

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!