如何运行由Elixir或Erlang编译的Beam文件? [英] How do I run a beam file compiled by Elixir or Erlang?

查看:599
本文介绍了如何运行由Elixir或Erlang编译的Beam文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经安装了Erlang / OTP和Elixir,并使用以下命令将HelloWorld程序编译为BEAM:

I have installed Erlang/OTP and Elixir, and compiled the HelloWorld program into a BEAM using the command:

elixirc test.ex

哪个生成了一个名为Elixir.Hello.beam的文件

Which produced a file named Elixir.Hello.beam

我如何运行此文件?

推荐答案

简短答案:在不确定的情况下也无法确定源文件的内容:)

Short answer: no way to know for sure without also knowing the contents of your source file :)

有几种方法可以运行Elixir代码。该答案将概述可与Elixir配合使用的各种工作流程。

There are a few ways to run Elixir code. This answer will be an overview of various workflows that can be used with Elixir.

当您刚刚开始并想尝试一下时,启动 iex 并一次评估一个表达式是必经之路。

When you are just getting started and want to try things out, launching iex and evaluating expressions one at a time is the way to go.

iex(5)> Enum.reverse [1,2,3,4]
[4, 3, 2, 1]

您还可以在 iex 中获得有关Elixir模块和功能的帮助。大多数功能在其文档中都有示例。

You can also get help on Elixir modules and functions in iex. Most of the functions have examples in their docs.

iex(6)> h Enum.reverse

                        def reverse(collection)

Reverses the collection.
[...]






何时您想将一些代码放入文件中以供日后重用,建议的(事实上是标准的)方法是创建一个混合项目并开始向其中添加模块。但是也许,您希望在依靠混合执行诸如编译代码,启动应用程序之类的常见任务之前,了解幕后情况。让我解释一下。


When you want to put some code into a file to reuse it later, the recommended (and de facto standard) way is to create a mix project and start adding modules to it. But perhaps, you would like to know what's going on under the covers before relying on mix to perform common tasks like compiling code, starting applications, and so on. Let me explain that.

将某些表达式放入文件并运行的最简单方法是使用 elixir 命令。

The simplest way to put some expressions into a file and run it would be to use the elixir command.

x = :math.sqrt(1234)
IO.puts "Your square root is #{x}"

将上述代码片段放入名为 simple的文件中.exs 并使用 elixir simple.exs 运行它。 .exs 扩展名只是一个约定,表示该文件应进行评估(这就是我们所做的)。

Put the above fragment of code into a file named simple.exs and run it with elixir simple.exs. The .exs extension is just a convention to indicate that the file is meant to be evaluated (and that is what we did).

这可以一直进行到要开始构建项目为止。然后,您需要将代码组织到模块中。每个模块都是功能的集合。它也是最小的编译单元:每个模块都被编译为 .beam 文件。通常,每个源文件只有一个模块,但是定义多个模块也是可以的。不管单个源文件中的模块数量如何,每个模块在编译时都将以其自己的 .beam 文件结尾。

This works up until the point you want to start building a project. Then you will need to organize your code into modules. Each module is a collection of functions. It is also the minimal compilation unit: each module is compiled into a .beam file. Usually people have one module per source file, but it is also fine to define more than one. Regardless of the number of modules in a single source file, each module will end up in its own .beam file when compiled.

defmodule M do
  def hi(name) do
    IO.puts "Hello, #{name}"
  end
end

我们定义了一个具有单个功能的模块。将其保存到名为 mymod.ex 的文件中。我们可以通过多种方式使用它:

We have defined a module with a single function. Save it to a file named mymod.ex. We can use it in multiple ways:


  • 启动 iex 并进行评估生成的shell会话中的代码:

  • launch iex and evaluate the code in the spawned shell session:

$ iex mymod.ex

iex> M.hi "Alex"
Hello, Alex
:ok


  • 在运行其他代码之前对其进行评估。例如,要在命令行上评估单个表达式,请使用 elixir -e< expr> 。您可以在其之前需要(基本上是评估并加载)一个或多个文件:

  • evaluate it before running some other code. For example, to evaluate a single expression on the command line, use elixir -e <expr>. You can "require" (basically, evaluate and load) one or more files before it:

    $ elixir -r mymod.ex -e 'M.hi "Alex"'
    Hello, Alex
    


  • 编译并让VM的代码加载工具找到它

  • compile it and let the code loading facility of the VM find it

    $ elixirc mymod.ex
    $ iex
    iex> M.hi "Alex"
    Hello, Alex
    :ok
    


  • 在最后一个示例中,我们编译了该模块,该模块在当前目录中生成了名为 Elixir.M.beam 的文件。然后,当您在同一目录中运行 iex 时,将在第一次调用该模块的函数时加载该模块。您还可以使用其他方式来评估代码,例如 elixir -e’M.hi ...'。只要代码加载器可以找到 .beam 文件,该模块就会被加载并执行其中的适当功能。

    In that last example we compiled the module which produced a file named Elixir.M.beam in the current directory. When you then run iex in the same directory, the module will be loaded the first time a function from it is called. You could also use other ways to evaluate code, like elixir -e 'M.hi "..."'. As long as the .beam file can be found by the code loader, the module will be loaded and the appropriate function in it will be executed.

    但是,这都是关于尝试一些代码示例的。当您准备在Elixir中构建项目时,您将需要使用 mix 。带有 mix 的工作流程大致如下:

    However, this was all about trying to play with some code examples. When you are ready to build a project in Elixir, you will need to use mix. The workflow with mix is more or less as follows:

    $ mix new myproj
    * creating README.md
    * creating .gitignore
    * creating mix.exs
    [...]
    
    $ cd myproj
    
    # 'mix new' has generated a dummy test for you
    # see test/myproj_test.exs
    $ mix test
    

    lib / 目录中添加新模块。通常会在所有模块名称前加上项目名称。因此,如果采用上面定义的 M 模块并将其放入文件 lib / m.ex 中,看起来像这样:

    Add new modules in the lib/ directory. It is customary to prefix all module names with your project name. So if you take the M module we defined above and put it into the file lib/m.ex, it'll look like this:

     defmodule Myproj.M do
       def hi(name) do
         IO.puts "Hello, #{name}"
       end
     end
    

    现在可以在加载了Mix项目的情况下启动shell。

    Now you can start a shell with the Mix project loaded in it.

     $ iex -S mix
    

    运行上面的命令将编译您的所有源文件,并将它们放在 _build 目录下。 Mix还将为您设置代码路径,以便代码加载器可以在该目录中找到 .beam 文件。

    Running the above will compile all your source file and will put them under the _build directory. Mix will also set up the code path for you so that the code loader can locate .beam files in that directory.

    在混合项目的上下文中评估表达式如下:

    Evaluating expressions in the context of a mix project looks like this:

    $ mix run -e 'Myproj.M.hi "..."'
    

    同样,无需编译任何内容。大多数混合任务都会重新编译所有更改的文件,因此可以放心地假设,当您从其中调用函数时,定义的任何模块都可用。

    Again, no need to compile anything. Most mix tasks will recompile any changed files, so you can safely assume that any modules you have defined are available when you call functions from them.

    运行 mix help 查看所有可用任务,而 mix help< task> 获取特定任务的详细说明。

    Run mix help to see all available tasks and mix help <task> to get a detailed description of a particular task.

    这篇关于如何运行由Elixir或Erlang编译的Beam文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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