Skip to main content
replaced http://mathematica.stackexchange.com/ with https://mathematica.stackexchange.com/
Source Link

As Nasser notes in a comment every Symbol has a context. You should be aware that Symbols are created during parsing. See: Local variables in Module leak into the Global contextLocal variables in Module leak into the Global context.

Alright, now that we worked out what you want here is the simplified answer:

We can perform a similar operation to BeginPackage with Block:

Block[{$ContextPath = {"runPrv`", "System`"}, $Context = "runPrv`"}, . . .]

We can combine this with Leonid's method from Is it possible to use Begin and End inside a Manipulate?Is it possible to use Begin and End inside a Manipulate? to keep contexts from being fully resolved until we are ready for evaluation inside the Block. (Note that Global` Symbols are still created, as discussed above, but they will not be defined.) I believe "runPrv`" may be left out of $ContextPath in our application so long as we don't change the $Context from "runPrv`" within the Block itself. Finally we have:

SetAttributes[runPrivate, HoldAllComplete];

runPrivate[code_] :=
  With[{body = MakeBoxes @ code},
    Block[{$ContextPath = {"System`"}, $Context = "runPrv`"},
      ToExpression @ body]]

Now:

runPrivate[
  Get["test.m"];
  a = 5;
  test[a]
]

5

Global Symbols a and test remain undefined:

?a
?test

Global`a

Global`test

As Nasser notes in a comment every Symbol has a context. You should be aware that Symbols are created during parsing. See: Local variables in Module leak into the Global context.

Alright, now that we worked out what you want here is the simplified answer:

We can perform a similar operation to BeginPackage with Block:

Block[{$ContextPath = {"runPrv`", "System`"}, $Context = "runPrv`"}, . . .]

We can combine this with Leonid's method from Is it possible to use Begin and End inside a Manipulate? to keep contexts from being fully resolved until we are ready for evaluation inside the Block. (Note that Global` Symbols are still created, as discussed above, but they will not be defined.) I believe "runPrv`" may be left out of $ContextPath in our application so long as we don't change the $Context from "runPrv`" within the Block itself. Finally we have:

SetAttributes[runPrivate, HoldAllComplete];

runPrivate[code_] :=
  With[{body = MakeBoxes @ code},
    Block[{$ContextPath = {"System`"}, $Context = "runPrv`"},
      ToExpression @ body]]

Now:

runPrivate[
  Get["test.m"];
  a = 5;
  test[a]
]

5

Global Symbols a and test remain undefined:

?a
?test

Global`a

Global`test

As Nasser notes in a comment every Symbol has a context. You should be aware that Symbols are created during parsing. See: Local variables in Module leak into the Global context.

Alright, now that we worked out what you want here is the simplified answer:

We can perform a similar operation to BeginPackage with Block:

Block[{$ContextPath = {"runPrv`", "System`"}, $Context = "runPrv`"}, . . .]

We can combine this with Leonid's method from Is it possible to use Begin and End inside a Manipulate? to keep contexts from being fully resolved until we are ready for evaluation inside the Block. (Note that Global` Symbols are still created, as discussed above, but they will not be defined.) I believe "runPrv`" may be left out of $ContextPath in our application so long as we don't change the $Context from "runPrv`" within the Block itself. Finally we have:

SetAttributes[runPrivate, HoldAllComplete];

runPrivate[code_] :=
  With[{body = MakeBoxes @ code},
    Block[{$ContextPath = {"System`"}, $Context = "runPrv`"},
      ToExpression @ body]]

Now:

runPrivate[
  Get["test.m"];
  a = 5;
  test[a]
]

5

Global Symbols a and test remain undefined:

?a
?test

Global`a

Global`test

deleted 11 characters in body
Source Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k

As Nasser notes in a comment every Symbol has a context. You should be aware that Symbols are created during parsing. See: Local variables in Module leak into the Global context.

Alright, now that we worked out what you want here is the simplified answer:

We can perform a similar operation to BeginPackage with Block:

Block[{$ContextPath = {"runPrv`", "System`"}, $Context = "runPrv`"}, . . .]

We can combine this with Leonid's method from Is it possible to use Begin and End inside a Manipulate? to keep contexts from being fully resolved until we are ready for evaluation inside the Block. (Note that Global` Symbols are still created, as discussed above, but they will not be defined.) Finally I believe "runPrv`" may be left out of $ContextPath in our application so long as we don't change the $Context from "runPrv`" within the Block itself. Finally we have:

SetAttributes[runPrivate, HoldAllComplete];

runPrivate[code_] :=
  With[{body = MakeBoxes @ code},
    Block[{$ContextPath = {"runPrv`", "System`"}, $$ContextPath = {"System`"}, $Context = "runPrv`"},
      ToExpression @ body]]

Now:

runPrivate[
  Get["test.m"];
  a = 5;
  test[a]
]

5

Global Symbols a and test remain undefined:

?a
?test

Global`a

Global`test

As Nasser notes in a comment every Symbol has a context. You should be aware that Symbols are created during parsing. See: Local variables in Module leak into the Global context.

Alright, now that we worked out what you want here is the simplified answer:

We can perform a similar operation to BeginPackage with Block:

Block[{$ContextPath = {"runPrv`", "System`"}, $Context = "runPrv`"}, . . .]

We can combine this with Leonid's method from Is it possible to use Begin and End inside a Manipulate? to keep contexts from being fully resolved until we are ready for evaluation inside the Block. (Note that Global` Symbols are still created, as discussed above, but they will not be defined.) Finally we have:

SetAttributes[runPrivate, HoldAllComplete];

runPrivate[code_] :=
  With[{body = MakeBoxes @ code},
    Block[{$ContextPath = {"runPrv`", "System`"}, $Context = "runPrv`"},
      ToExpression @ body]]

Now:

runPrivate[
  Get["test.m"];
  a = 5;
  test[a]
]

5

Global Symbols a and test remain undefined:

?a
?test

Global`a

Global`test

As Nasser notes in a comment every Symbol has a context. You should be aware that Symbols are created during parsing. See: Local variables in Module leak into the Global context.

Alright, now that we worked out what you want here is the simplified answer:

We can perform a similar operation to BeginPackage with Block:

Block[{$ContextPath = {"runPrv`", "System`"}, $Context = "runPrv`"}, . . .]

We can combine this with Leonid's method from Is it possible to use Begin and End inside a Manipulate? to keep contexts from being fully resolved until we are ready for evaluation inside the Block. (Note that Global` Symbols are still created, as discussed above, but they will not be defined.) I believe "runPrv`" may be left out of $ContextPath in our application so long as we don't change the $Context from "runPrv`" within the Block itself. Finally we have:

SetAttributes[runPrivate, HoldAllComplete];

runPrivate[code_] :=
  With[{body = MakeBoxes @ code},
    Block[{$ContextPath = {"System`"}, $Context = "runPrv`"},
      ToExpression @ body]]

Now:

runPrivate[
  Get["test.m"];
  a = 5;
  test[a]
]

5

Global Symbols a and test remain undefined:

?a
?test

Global`a

Global`test

added 668 characters in body
Source Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k

I find this question rather enigmatic. AsAs Nasser notes in a comment every Symbol has a context. You should be aware that Symbols are created during parsing. See: Local variables in Module leak into the Global context.

You also say "The following works" so I wonder why you are not usingAlright, now that. Perhaps we worked out what you want:

SetAttributes[call, HoldAll];

call[file_String, function_, args___] :=
  Block[{$ContextPath, function}, Get[file]; function[args]]

Use would be:

call["test.m", test, "thing to print"]

thing to print

Note that the symbol Global`test here is created as described in the linked Q&A above, but it is not defined.simplified answer:


 

On reflection, perhaps you just want to load definitions intoWe can perform a private context (e.g. test` ) that is not on thesimilar operation to $ContextPath. That can be done like thisBeginPackage with Block:

Block[{$ContextPath = {"test`", "System`"}, $$ContextPath = {"runPrv`", "System`"}, $Context = "test`""runPrv`"}, Get["test.m"]]

This is essentially equivalent to BeginPackage but it works as a single line of code.

The defined function is then called with test`test:

test`test["thing 2"]. .]

thing 2


Incorporating the last round of comments, you may find use inWe can combine this function which combines that shown above andwith Leonid's method from Is it possible to use Begin and End inside a Manipulate? to keep contexts from being fully resolved until we are ready for evaluation inside the Block. (Note that Global` Symbols are still created, as discussed above, but they will not be defined.) Finally we have:

SetAttributes[runPrivate, HoldAllComplete];

runPrivate[code_] :=
  With[{body = MakeBoxes @ code},
    Block[{$ContextPath = {"runPrv`", "System`"}, $Context = "runPrv`"},
      ToExpression @ body]]

Now:

runPrivate[Get["testrunPrivate[
  Get["test.m"];
  a = 5; 
 test[a]] test[a]
]

5

Global Symbols a and test remain undefined:

?a
?test

Global`a

Global`test

I find this question rather enigmatic. As Nasser notes in a comment every Symbol has a context. You should be aware that Symbols are created during parsing. See: Local variables in Module leak into the Global context.

You also say "The following works" so I wonder why you are not using that. Perhaps you want:

SetAttributes[call, HoldAll];

call[file_String, function_, args___] :=
  Block[{$ContextPath, function}, Get[file]; function[args]]

Use would be:

call["test.m", test, "thing to print"]

thing to print

Note that the symbol Global`test is created as described in the linked Q&A above, but it is not defined.


 

On reflection, perhaps you just want to load definitions into a private context (e.g. test` ) that is not on the $ContextPath. That can be done like this:

Block[{$ContextPath = {"test`", "System`"}, $Context = "test`"}, Get["test.m"]]

This is essentially equivalent to BeginPackage but it works as a single line of code.

The defined function is then called with test`test:

test`test["thing 2"]

thing 2


Incorporating the last round of comments, you may find use in this function which combines that shown above and Leonid's method from Is it possible to use Begin and End inside a Manipulate?:

SetAttributes[runPrivate, HoldAllComplete];

runPrivate[code_] :=
  With[{body = MakeBoxes @ code},
    Block[{$ContextPath = {"runPrv`", "System`"}, $Context = "runPrv`"},
      ToExpression @ body]]

Now:

runPrivate[Get["test.m"]; a = 5; test[a]]

5

Global Symbols a and test remain undefined:

?a
?test

Global`a

Global`test

As Nasser notes in a comment every Symbol has a context. You should be aware that Symbols are created during parsing. See: Local variables in Module leak into the Global context.

Alright, now that we worked out what you want here is the simplified answer:

We can perform a similar operation to BeginPackage with Block:

Block[{$ContextPath = {"runPrv`", "System`"}, $Context = "runPrv`"}, . . .]

We can combine this with Leonid's method from Is it possible to use Begin and End inside a Manipulate? to keep contexts from being fully resolved until we are ready for evaluation inside the Block. (Note that Global` Symbols are still created, as discussed above, but they will not be defined.) Finally we have:

SetAttributes[runPrivate, HoldAllComplete];

runPrivate[code_] :=
  With[{body = MakeBoxes @ code},
    Block[{$ContextPath = {"runPrv`", "System`"}, $Context = "runPrv`"},
      ToExpression @ body]]

Now:

runPrivate[
  Get["test.m"];
  a = 5; 
  test[a]
]

5

Global Symbols a and test remain undefined:

?a
?test

Global`a

Global`test

added 668 characters in body
Source Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k
Loading
added 364 characters in body
Source Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k
Loading
Source Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k
Loading