# Calling function of included file

**URL:** <https://discourse.julialang.org/t/calling-function-of-included-file/20137>\
**Category:** New to Julia\
**Created:** [January 26, 2019, 11:28pm UTC](https://discourse.julialang.org/t/calling-function-of-included-file/20137 "2019-01-26T23:28:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![MrProgrammer](https://avatars.discourse-cdn.com/v4/letter/m/7feea3/32.png) [@MrProgrammer](https://discourse.julialang.org/u/MrProgrammer)\
**Post date:** [January 26, 2019, 11:28pm UTC](https://discourse.julialang.org/t/calling-function-of-included-file/20137/1 "2019-01-26T23:28:15Z")

</div>

I’m looking to include load some files dynamically and then call a function on the newly created file.

here is the file I’m trying to include

```julia
module SetupModule

function setup()
    println("------------setup called")
# end module
end

```

Here is the module calling setup()

```julia
function registerFactory()
    factoryPaths::Array{String} = ioAntigenFS.getPathsBySufffix("./core/factories", "Factory.jl")
    for path in factoryPaths
        @debug ("Including path: " * path)
        include(path)
        setup()
    end
end

```

So when I try to run the setup() function of the SetupModule from the registerFactory function I see the below error.

```julia
ERROR: LoadError: UndefVarError: setup not defined

```

From what I read, `include` just included the file verbatim, so the function should be in scope.

If I remove the module declaration from the SetUp module then I see a different error.

```julia
(method too new to be called from this world context.)

```

Wondering if there is a way to include a Julia source file and execute one of it’s functions.

---

<div class="post-metadata">

**Author:** ![lostella](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lostella/32/356_2.png) [@lostella](https://discourse.julialang.org/u/lostella)\
**Post date:** [January 26, 2019, 11:54pm UTC](https://discourse.julialang.org/t/calling-function-of-included-file/20137/2 "2019-01-26T23:54:53Z")

</div>

The file you are including defines a `module` with the `setup` function inside.

Therefore you should be using the fully qualified function name, that is `SetupModule.setup` to invoke it.

---

<div class="post-metadata">

**Author:** ![MrProgrammer](https://avatars.discourse-cdn.com/v4/letter/m/7feea3/32.png) [@MrProgrammer](https://discourse.julialang.org/u/MrProgrammer)\
**Post date:** [January 27, 2019, 12:21am UTC](https://discourse.julialang.org/t/calling-function-of-included-file/20137/3 "2019-01-27T00:21:39Z")

</div>

I’ve made the suggested modifications, but in order to get them to work I have to use the @eval macro.

The docs just say that @eval evaluates an expression. Does this mean that include brings in the source code of a file unevaluated, and it has to be evaluated manually?

```julia
function registerFactory()
    factoryPaths::Array{String} = ioAntigenFS.getPathsBySufffix("./core/factories", "Factory.jl")
    for path in factoryPaths
        @debug ("Including path: " * path)
        include(path)
        @eval(SetupModule.setup())
    end
end

```

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [January 27, 2019, 12:32am UTC](https://discourse.julialang.org/t/calling-function-of-included-file/20137/4 "2019-01-27T00:32:27Z")

</div>

> [@MrProgrammer](#):
>
> Does this mean that include brings in the source code of a file unevaluated, and it has to be evaluated manually?

No, you’re essentially running into [Methods · The Julia Language](https://docs.julialang.org/en/v0.7.0/manual/methods/#Redefining-Methods-1) . The methods defined when you `include()` something are “too new”, just like the `@eval newfun() = 1` definition in the example from the documentation. The solution from the documentation will also work here: you should be able to do something like:

```julia
...
include(path)
Base.invokelatest(SetupModule.setup)
...

```

---

<div class="post-metadata">

**Author:** ![MrProgrammer](https://avatars.discourse-cdn.com/v4/letter/m/7feea3/32.png) [@MrProgrammer](https://discourse.julialang.org/u/MrProgrammer)\
**Post date:** [January 27, 2019, 1:03pm UTC](https://discourse.julialang.org/t/calling-function-of-included-file/20137/5 "2019-01-27T13:03:21Z")

</div>

You hit the nail on the head.  
The error makes sense now.
