# How can I control what variables are in scope

**URL:** <https://discourse.julialang.org/t/how-can-i-control-what-variables-are-in-scope/81622>\
**Category:** New to Julia\
**Tags:** metaprogramming\
**Created:** [May 25, 2022, 12:27am UTC](https://discourse.julialang.org/t/how-can-i-control-what-variables-are-in-scope/81622 "2022-05-25T00:27:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidstreader](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidstreader/32/35704_2.png) [@davidstreader](https://discourse.julialang.org/u/davidstreader)\
**Post date:** [May 25, 2022, 12:27am UTC](https://discourse.julialang.org/t/how-can-i-control-what-variables-are-in-scope/81622/1 "2022-05-25T00:27:41Z")

</div>

Hi I am writing a Domain Specific Language and want to check that an Expr is formatted correctly.  
I have been trying to use **eval(ex)**. But this evaluates the expression with access to variables in global scope.  
Is there a way to: (a) list the variables in global scope so that I can clear them?  
(b) clear all variables from global scope?  
(c) evaluate an expression with restricted scope?  
I may have missed something obvious , like going about this in the wrong way, so any pointers very much appreciated. david

I could preprocess variable names and that way fake variable scope but this seems like an ugly solution.

---

<div class="post-metadata">

**Author:** ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)\
**Post date:** [May 25, 2022, 1:00am UTC](https://discourse.julialang.org/t/how-can-i-control-what-variables-are-in-scope/81622/2 "2022-05-25T01:00:01Z")

</div>

How about

```julia
Module Emptymod
end

expr = ...
Emptymod.eval(expr)

```

(Untested, sent from phone)

---

<div class="post-metadata">

**Author:** ![davidstreader](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidstreader/32/35704_2.png) [@davidstreader](https://discourse.julialang.org/u/davidstreader)\
**Post date:** [May 25, 2022, 1:11am UTC](https://discourse.julialang.org/t/how-can-i-control-what-variables-are-in-scope/81622/3 "2022-05-25T01:11:37Z")

</div>

Thanks Peter nice try but  
module EmptyMod  
end  
xex = 2;  
EmptyMod.eval(1+xex)  
evaluates to 3

I will explore the idea EmptyMod.eval not thought of this before.

YES!  
module EmptyMod  
function runClean(ex)  
go = eval(ex)  
println("runClean ",go)  
return go  
end  
end  
x = 22;  
sol = EmptyMod.runClean(:(1+x));  
is most of the way there! thanks again

Still not working as Julia no longer supports clear!(:x). The only way I can see is to use JuMp as they also need to add and delete variables. This seems like a some what heavy handed use of JuMP as my first step is to visualise Symbolic Petri Nets only then will I be considering JuMP style constaint solving. So have I missed something or is evaluating Expr in a controlled environment just hard? any ideas much appreciated.

---

<div class="post-metadata">

**Author:** ![davidstreader](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidstreader/32/35704_2.png) [@davidstreader](https://discourse.julialang.org/u/davidstreader)\
**Post date:** [May 25, 2022, 3:34am UTC](https://discourse.julialang.org/t/how-can-i-control-what-variables-are-in-scope/81622/5 "2022-05-25T03:34:18Z")

</div>

Looks like the whole eval approach is now obsolete! and best practice is to use Modia @model.  
So I will start again. david
