# Is there difference between Meta.eval and eval?

**URL:** https://discourse.julialang.org/t/is-there-difference-between-meta-eval-and-eval/27392
**Category:** New to Julia
**Tags:** metaprogramming
**Created:** [August 10, 2019, 2:32pm UTC](https://discourse.julialang.org/t/is-there-difference-between-meta-eval-and-eval/27392 "2019-08-10T14:32:51Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Dito](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dito/32/10039_2.png) [@Dito](https://discourse.julialang.org/u/Dito)
#### Post date: [August 10, 2019, 2:32pm UTC](https://discourse.julialang.org/t/is-there-difference-between-meta-eval-and-eval/27392/1 "2019-08-10T14:32:51Z")

</div>

```julia
ex=quote
    x=1
    y=2
    x+y
end
ex.args
ex.head
Meta.eval(ex.args[6])
eval(ex.args[6])

```

```julia
julia> ex.head
:block

julia> Meta.eval(ex.args[6])
ERROR: UndefVarError: x not defined
Stacktrace:
 [1] top-level scope at none:0
 [2] eval at ./boot.jl:328 [inlined]
 [3] eval(::Expr) at ./meta.jl:6
 [4] top-level scope at none:0

julia> eval(ex.args[6])
3

```

I don’t understand

---

<div class="post-metadata">

### Author: ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)
#### Post date: [August 10, 2019, 3:47pm UTC](https://discourse.julialang.org/t/is-there-difference-between-meta-eval-and-eval/27392/2 "2019-08-10T15:47:52Z")

</div>

Every module has a function `Module.eval` that evaluates code in the scope of the module. `x` hasn’t been defined in the module `Meta`, so `Meta.eval(ex.args[6])` throws an error. The reason why `eval(ex.args[6])` works is probably because you defined `x` before in the same session. You can read more about how `eval` works here: [https://docs.julialang.org/en/v1/manual/metaprogramming/index.html#QuoteNode-1](https://docs.julialang.org/en/v1/manual/metaprogramming/index.html#QuoteNode-1)

---

<div class="post-metadata">

### Author: ![Dito](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dito/32/10039_2.png) [@Dito](https://discourse.julialang.org/u/Dito)
#### Post date: [August 30, 2019, 9:49am UTC](https://discourse.julialang.org/t/is-there-difference-between-meta-eval-and-eval/27392/3 "2019-08-30T09:49:38Z")

</div>

Now I understand. Thanks ❤

```julia
julia> ex=:(x₁=1)
:(x₁ = 1)

julia> x₁
ERROR: UndefVarError: x₁ not defined

julia> Meta.x₁
ERROR: UndefVarError: x₁ not defined

julia> Meta.eval(ex)
1

julia> x₁
ERROR: UndefVarError: x₁ not defined

julia> Meta.x₁
1

julia> eval(ex)
1

julia> x₁
1

```

**Meta**.eval() creates local variable x₁ in module **Meta** - ( **Meta**.x₁) , but eval() → global variables

---

<div class="post-metadata">

### Author: ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)
#### Post date: [August 30, 2019, 10:24am UTC](https://discourse.julialang.org/t/is-there-difference-between-meta-eval-and-eval/27392/4 "2019-08-30T10:24:22Z")

</div>

> [@Dito](#):
>
> **Meta**.eval() creates local variable x₁ in module **Meta** - ( **Meta**.x₁) , but eval() → global variables

`Module.eval(expr)` evaluates `expr` in the global scope of `Module`.  
`eval(expr)` evaluates `expr` in the global scope of the “current” module.

If `expr` creates a variable, as in your example `:(x₁=1)`, it will be created as a global variable.
