官术网_书友最值得收藏!

Loading and compiling modules

It is possible to load code from source files into an IEx session. Multiple modules may be loaded and used, allowing us to incorporate existing code into our prototyping or idea testing session.

Getting ready

In this recipe, we will be importing two files that define the Greeter and Echoer modules into our IEx session.

In the following lines, we will list the contents of these modules:

code\greeter.ex
defmodule Greeter do

  def greet(name \\ "you") do
    "Hello #{name} !"
  end

end

code/echoer.ex
defmodule Echoer do

  def echo(msg) do
    IO.puts "#{msg} ... #{msg} ...... #{msg}"
  end

end

Tip

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

How to do it…

We will follow these steps to load and compile the modules:

  1. Start IEx:
    iex
    
  2. Load the Greeter module defined in greeter.ex:
    iex(1)> c("greeter.ex")
    [Greeter]
    
  3. Load the Echoer module defined in echoer.ex:
    iex(2)> c("echoer.ex")
    [Echoer]
    
  4. Use the greet function defined in the Greeter module:
    iex(3)> Greeter.greet("Me")
    "Hello Me !"
    
  5. Use the echo function defined in the Echoer module:
    iex(4)> Echoer.echo("hello")
    hello ... hello ...... hello
    :ok
    
  6. Combine the functions defined in both modules:
    iex(7)> Greeter.greet("Me") |> Echoer.echo
    Hello Me ! ... Hello Me ! ...... Hello Me !
    :ok
    

Note

Some functions may have default values. They are denoted by the use of \\. In the Greeter module, the greet function is defined as def greet(name \\ "you"), which means that if we omit the argument passed to the function, it will default to you.

How it works…

When c("file_name.ex") is invoked from IEx, the file is loaded and compiled (a corresponding file with the .beam extension will be created).

The module (or modules) defined on each imported file become available. It is possible to invoke functions on these modules using the ModuleName.function_name(args) syntax.

If a module_name.beam file exists for a given module, then every time you import that module into an IEx session, you will see the following warning:

module_name.ex:1: warning: redefining module ModuleName

The warning means that a new compiled .beam file is being created, potentially redefining the module. If no changes were made to the source code, the code will be the same, although the warning is still issued.

In step 6, the pipe operator (|>) is used to simplify the code. This operator means that the output of the left operation will be fed as the first argument to the right operation.

This is equivalent to writing the following:

Echoer.echo(Greeter.greet("Me"))

There's more…

In steps 2 and 3, the greeter.ex and echoer.ex files are imported without indicating the path because they are under the same directory from where the IEx session was started.

It is possible to use relative or full paths when loading files:

  • We can use relative paths like this:
    iex(1)> c("../greeter.ex")
    
  • We can use full paths like this:
    iex(2)> c("/home/user/echoer.ex")
    

    Note

    Note that the c IEx function accepts a string as an argument.

主站蜘蛛池模板: 武夷山市| 沙田区| 昌邑市| 榆树市| 大渡口区| 包头市| 白山市| 将乐县| 鞍山市| 佳木斯市| 大余县| 平利县| 兴文县| 仁化县| 南阳市| 天台县| 灵石县| 蚌埠市| 于田县| 溆浦县| 杭锦后旗| 西宁市| 郓城县| 英吉沙县| 雷波县| 铁岭县| 朝阳县| 惠东县| 紫云| 南漳县| 溆浦县| 武隆县| 陆川县| 资兴市| 天气| 竹溪县| 邵阳市| 武平县| 新郑市| 福建省| 麻城市|