# Include a script from external file in different scopes

**URL:** https://discourse.julialang.org/t/include-a-script-from-external-file-in-different-scopes/1431
**Category:** General Usage
**Tags:** question
**Created:** [January 12, 2017, 12:50pm UTC](https://discourse.julialang.org/t/include-a-script-from-external-file-in-different-scopes/1431 "2017-01-12T12:50:17Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![ucecmqu](https://avatars.discourse-cdn.com/v4/letter/u/858c86/32.png) [@ucecmqu](https://discourse.julialang.org/u/ucecmqu)
#### Post date: [January 12, 2017, 12:50pm UTC](https://discourse.julialang.org/t/include-a-script-from-external-file-in-different-scopes/1431/1 "2017-01-12T12:50:17Z")

</div>

Hello everyone,  
I am currently working on a script in which the same function “g” has to be evaluated multiple times, also within other functions.

```julia
1. a=3

2. function g(y)
3. a*y
4. end

5. function f(x,y)
6. function g(y)
7. a*y
8. end
9. a=4
10. x+g(y)
11. end

12. println(g(3))
13. println(f(1,3))

```

This function “g” depends on both the argument “y” passed to the function itself and a certain parameter “a”. The script above does exactly what I want, indeed, the command println(g(3)) returns value 9, evaluated adopting parameter a=3 defined in the Main module, while println(f(1,3)) returns 13, meaning that when “g” is called within function “f”, this is evaluated with the value of a=4 defined in the scope of function “f”.

In the script on which I am working, “g” appears many times and every time I want it to be evaluated with the parameter “a” defined in the scope of the caller, without passing “a” as an argument to the function.

Since “g” has always the same form I would like to import it from an external file “g.jl” in order to make modifications to the form of “g” easier to implement. The script may appear as follows:

file g.jl:

```julia
1. function g(y)
2. a*y
3. end

```

file myscript.jl:

```julia
1. a=3

2. include("g.jl")

3. function f(x,y)
4. include("g.jl")
5. a=4
6. x+g(y)
7. end

8. println(g(3))
9. println(f(1,3))

```

However now, the command println(f(1,3)) returns 10 and not 13, meaning that when “g” is called, that is evaluated adopting a=3 from the main. So my question is: is there any way of including a script present in an external file executing it only in the scope from which it is called? Hope my question is clear. Thanks to anybody willing to help.

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [January 12, 2017, 12:55pm UTC](https://discourse.julialang.org/t/include-a-script-from-external-file-in-different-scopes/1431/2 "2017-01-12T12:55:40Z")

</div>

Include always works at the global scope, just like `eval`. You can use a macro:

```julia
macro insert_g()
  quote
    function g(y)
      a*y
    end
  end
end

...
function f(x,y)
   @insert_g
    ...
end

```

---

<div class="post-metadata">

### Author: ![ucecmqu](https://avatars.discourse-cdn.com/v4/letter/u/858c86/32.png) [@ucecmqu](https://discourse.julialang.org/u/ucecmqu)
#### Post date: [January 12, 2017, 1:42pm UTC](https://discourse.julialang.org/t/include-a-script-from-external-file-in-different-scopes/1431/3 "2017-01-12T13:42:45Z")

</div>

Thanks a lot mauro3! The suggestion of using macro is great, but I could not run the code you posted. I have modified it as follows and it works perfectly:

```julia
1. a=3

2. macro include_g()
3. return :(function g(y) a*y end)
4. end

5. @include_g

6. function f(x,y)
7. @include_g
8. a=4
9. x+g(y)
10. end

11. println(g(3))
12. println(f(1,3))

```

Your help has been fundamental, many thanks!

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [January 12, 2017, 2:09pm UTC](https://discourse.julialang.org/t/include-a-script-from-external-file-in-different-scopes/1431/4 "2017-01-12T14:09:05Z")

</div>

Note that this macro definition is wrong. `g` must be escaped.

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [January 12, 2017, 2:40pm UTC](https://discourse.julialang.org/t/include-a-script-from-external-file-in-different-scopes/1431/5 "2017-01-12T14:40:21Z")

</div>

Here’s a macro which generates macros like that:

> [@\`@def\` macro generator broken on master](https://discourse.julialang.org/t/def-macro-generator-broken-on-master/1096/3):
>
> I see, name wasn’t escaped: macro def(name, definition) return quote macro $(esc(name))() esc($(Expr(:quote, definition))) end end end @def test begin a = 2 end # Now @test "pastes" the code `a=2` is good now.

Use with caution. (The fixed version includes the escaping @yuyichao mentions).

---

<div class="post-metadata">

### Author: ![ucecmqu](https://avatars.discourse-cdn.com/v4/letter/u/858c86/32.png) [@ucecmqu](https://discourse.julialang.org/u/ucecmqu)
#### Post date: [January 13, 2017, 11:45am UTC](https://discourse.julialang.org/t/include-a-script-from-external-file-in-different-scopes/1431/6 "2017-01-13T11:45:43Z")

</div>

Very interesting suggestions, many thanks to everyone! I apologise because I am quite new to Julia, but could someone explain to me why does the code I wrote works fine even without escaping the function.

Or, in other words, what is the risk of using the code I propose?

```julia
1. a=3

2. macro include_g()
3. return :(function g(y) a*y end)
4. end

5. @include_g

6. function f(x,y)
7. @include_g
8. a=4
9. x+g(y)
10. end

11. println(g(3))
12. println(f(1,3))

```

many thanks!

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [January 13, 2017, 1:08pm UTC](https://discourse.julialang.org/t/include-a-script-from-external-file-in-different-scopes/1431/7 "2017-01-13T13:08:12Z")

</div>

> [@ucecmqu](#):
>
> but could someone explain to me why does the code I wrote works fine even without escaping the function.

Because you are relying on a bug in \< 0.6.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [January 13, 2017, 2:48pm UTC](https://discourse.julialang.org/t/include-a-script-from-external-file-in-different-scopes/1431/8 "2017-01-13T14:48:43Z")

</div>

> [@ucecmqu](#):
>
> In the script on which I am working, “g” appears many times and every time I want it to be evaluated with the parameter “a” defined in the scope of the caller, without passing “a” as an argument to the function.

A better way to do this is just to write your “script” as a function that takes `g` as an argument:

```jl
function mystuff(g, ...other args...)
     ... do stuff with g(x) ...
end

```

Then, define your actual function as

```julia
function g(x, a)
     return x * a # or whatever
end

```

and call `mystuff` for different `a` values by constructing anonymous functions:

```julia
mystuff(x -> g(x, a1), ...)
mystuff(x -> g(x, a2), ...)

```

and so on.

In general, if you find yourself messing around with macros and code generation just to call a function with different parameters, you are probably making a mistake.

Note also that using global variables like `a=3` kills performance (and is problematic from a software-engineering standpoint as well).
