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



$$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]] &;

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

$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! :)

  • $\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

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, 
        {expr$ = ToString[Unevaluated[expr], InputForm]},
  • $\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$ 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.