4
$\begingroup$

I am introducing the mathematica Security (MSP module) to our system. The Document suggests use MSPToExpression as followed:

Needs["MSP`"]

SetSecurity[];

$$var = "5+7";

MSPToExpression[ $$var] (*the output is 12*)

But when I tried to adapt this function to $PrePrint with scripts:

$PrePrint = With[{$$var = ToString[#, InputForm]}, MSPToExpression[$$var]] &;
(*or*)

$PrePrint = With[{expr = #}, MSPToExpression[expr]] &;
(*or*)

$PrePrint = With[{$$var = #}, MSPToExpression[$$var]] &;

Input: 5+7

there's same error says: enter image description here

It can't recognize $$var as symbol, can anyone help me on this? Many thanks! :)

$\endgroup$
  • $\begingroup$ What if you use Module? With injects expressions to held slots. $\endgroup$ – Kuba Nov 27 '15 at 7:49
  • $\begingroup$ @Kuba Hi, Could you please paste the full script ? I am a real newbee for mathematica. $\endgroup$ – Michael Nov 27 '15 at 7:57
4
$\begingroup$

I suppose this is what you need.

So the first thing is to use Module since With injects values into held expressions.

Moreover PrePrint is probably to late to catch "5+7". So I'd go with $Pre:

$Pre = Function[expr, 
  Module[
        {expr$ = ToString[Unevaluated[expr], InputForm]},
         MSPToExpression[expr$]
  ],
  HoldAllComplete
]
|improve this answer|||||
$\endgroup$
  • $\begingroup$ Thanks! It works well. it's what I was looking for . $\endgroup$ – Michael Nov 27 '15 at 8:29
  • 1
    $\begingroup$ Watch out when using HoldAll instead of HoldAllComplete in such functions! Evaluate[5 + 7] then evaluates to MSPToExpression["12"] $\endgroup$ – Jacob Akkerboom Nov 27 '15 at 11:33
  • $\begingroup$ @JacobAkkerboom good point. $\endgroup$ – Kuba Nov 27 '15 at 11:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.