Please, consider
$Pre = If[go, #, Null] &;
Now, this
go = True;
a = 1;
b = 1;
go = TrueQ @ (b != 0);
a/b
returns 1
, and this
go = True;
a = 1;
b = 0;
go = TrueQ @ (b != 0);
a/b
throws a Power::infy
message. The reason is that: Unless $Pre
is assigned to be a function which holds its arguments unevaluated, input expressions will be evaluated before $Pre
is applied (from Documentation Center)
Let's consider, instead
$Pre = If[go, #, Null] &;
go = True;
SetAttributes[$Pre, HoldAll];
This still give 1
go = True;
a = 1;
b = 1;
go = TrueQ @ (b != 0);
a/b
but this
go = True;
a = 1;
b = 0;
go = TrueQ @ (b != 0);
a/b
should give Null
. On the contrary, the error message appears again. Can you, please, explain why ?
addendum
With
$Pre = Function[{arg}, If[go, arg, Null], HoldAll];
by
go = True;
a = 1;
b = 1;
go = TrueQ@(b != 0);
a/b
I get
If[go,go=True;,Null]
If[go,a=1;,Null]
If[go,b=1;,Null]
If[go,go=TrueQ[b!=0];,Null]
If[go,a/b,Null]
and
go = True;
a = 1;
b = 0;
go = TrueQ@(b != 0);
a/b
returns
If[go,go=True;,Null]
If[go,a=1;,Null]
If[go,b=0;,Null]
If[go,go=TrueQ[b!=0];,Null]
If[go,a/b,Null]
$Pre
should be a function with attributeHoldAll
; in your case, you could try$Pre = Function[{arg}, If[go, arg, Null], HoldAll]
. I haven't tested it yet though. $\endgroup$