# Differences of Core.eval vs. running the code directly in other modules then Main

**URL:** <https://discourse.julialang.org/t/differences-of-core-eval-vs-running-the-code-directly-in-other-modules-then-main/82489>\
**Category:** General Usage\
**Tags:** documenter, metaprogramming, eval, modules\
**Created:** [June 9, 2022, 11:19am UTC](https://discourse.julialang.org/t/differences-of-core-eval-vs-running-the-code-directly-in-other-modules-then-main/82489 "2022-06-09T11:19:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sfuerst](https://avatars.discourse-cdn.com/v4/letter/s/3da27b/32.png) [@sfuerst](https://discourse.julialang.org/u/sfuerst)\
**Post date:** [June 9, 2022, 11:19am UTC](https://discourse.julialang.org/t/differences-of-core-eval-vs-running-the-code-directly-in-other-modules-then-main/82489/1 "2022-06-09T11:19:52Z")

</div>

While writing the documentation of a package I’m currently developing, I did ran into a problem with Documenter.jl which I could track down to the sandboxing mechanism of Documenter in combination with Core.eval.

Without explaining the context here the MWE:

This code works:

```julia
function bar()
    Main.Sandbox.Foo()
end

module Sandbox
struct Foo end
Main.bar()
end

```

But when you call the Main.bar() via Core.eval you get an “Sandbox is not defined message”:

```julia
function bar()
    Main.Sandbox.Foo()
end

expr = Meta.parseall("""
struct Foo end
Main.bar()
""")

Core.eval(Module(:Sandbox), expr)

```

If the code is evaluated in Main, everything works fine again:

```julia
function bar()
    Main.Foo()
end

expr = Meta.parseall("""
struct Foo end
Main.bar()
""")

Core.eval(Main, expr)

```

Can someone explain, where the different behavior comes from?

Thanks

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [June 9, 2022, 12:58pm UTC](https://discourse.julialang.org/t/differences-of-core-eval-vs-running-the-code-directly-in-other-modules-then-main/82489/2 "2022-06-09T12:58:52Z")

</div>

The problem is that `Module(:Sandbox)` apparently makes a module named `Sandbox`, but does not bind it to the name `Sandbox`:

```julia
julia> Module(:Baz)
Main.Baz

julia> Baz
ERROR: UndefVarError: Baz not defined

```

It does work, though, if you do the binding explicitly:

```julia
Sandbox = Module(:Sandbox)
Core.eval(Sandbox, expr)

```

(and using an alias would work as well):

```julia
Alias = Module(:Sandbox)
Core.eval(Alias, expr)

```

---

<div class="post-metadata">

**Author:** ![sfuerst](https://avatars.discourse-cdn.com/v4/letter/s/3da27b/32.png) [@sfuerst](https://discourse.julialang.org/u/sfuerst)\
**Post date:** [June 9, 2022, 3:55pm UTC](https://discourse.julialang.org/t/differences-of-core-eval-vs-running-the-code-directly-in-other-modules-then-main/82489/3 "2022-06-09T15:55:54Z")

</div>

Thank you, that is a good hint. But to be honest, I do not understand, why the binding is necessary, and what makes it even worse, why it’s not enough to bind it to a function local variable, as this is happening in Documenter.jl:

```julia
function bar()
    Main.Sandbox.Foo()
end

function test()
    expr = Meta.parseall("""
    struct Foo end
    Main.bar()
    """)

    Sandbox = Module(:Sandbox)
    Core.eval(Sandbox, expr)
end

test()

```

fails again with a "UndefVarError: Sandbox not defined), it is necessary to make the Sandbox binding global.

---

<div class="post-metadata">

**Author:** ![sfuerst](https://avatars.discourse-cdn.com/v4/letter/s/3da27b/32.png) [@sfuerst](https://discourse.julialang.org/u/sfuerst)\
**Post date:** [June 9, 2022, 7:32pm UTC](https://discourse.julialang.org/t/differences-of-core-eval-vs-running-the-code-directly-in-other-modules-then-main/82489/4 "2022-06-09T19:32:57Z")

</div>

I have the impression that using an alias does not work. Did you restart the session?

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [June 9, 2022, 9:27pm UTC](https://discourse.julialang.org/t/differences-of-core-eval-vs-running-the-code-directly-in-other-modules-then-main/82489/5 "2022-06-09T21:27:47Z")

</div>

> [@sfuerst](#):
>
> I do not understand, why the binding is necessary

I don’t know the internals, but it seems to me that separating the creation of a module (which needs a fixed name) and the binding to another module’s namespace (possibly with another name) may be necessary to allow things like this:

```julia
julia> import Random as RNDM # I might want `Main.Random` to be a different thing

julia> @isdefined Random
false

julia> RNDM.eval(:@ __MODULE__ ) # But this is the "real" name of the module
Random

```

With `Module(:Sandbox)` you are creating the module called `Sandbox`, but still have the possibility of using `Main.Sandbox` for a different thing, as in the previous example with the module `Random`.

> [@sfuerst](#):
>
> why it’s not enough to bind it to a function local variable

I don’t know the answer to that, but I had never seen a module defined as a local object of a function - I didn’t even imagine that it could be allowed.

You comment that this is related to Documenter.jl, whose internals I don’t know, so that may be the reason of my failure to understand what you are trying to do. Could this other topic be related to the problem you report?

> [@Pass local module to a function?](https://discourse.julialang.org/t/pass-local-module-to-a-function/40021):
>
> I’m trying to create a minimal working example of a Documenter error I’m experiencing. However I’m having trouble passing the “local” module to the doctest function, This is what I’m trying: using Documenter module Foo function foo() end end doctest(Foo) I’ve also tried “.Foo”, “…Foo”, “…Foo”, and “Main.Foo”. Is this even possible? Or do I need to create a module somewhere and use Pkg to add it?
