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$
2
  • $\begingroup$ What if you use Module? With injects expressions to held slots. $\endgroup$
    – Kuba
    Commented Nov 27, 2015 at 7:49
  • $\begingroup$ @Kuba Hi, Could you please paste the full script ? I am a real newbee for mathematica. $\endgroup$
    – Michael
    Commented Nov 27, 2015 at 7:57

1 Answer 1

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
]
$\endgroup$
3
  • $\begingroup$ Thanks! It works well. it's what I was looking for . $\endgroup$
    – Michael
    Commented Nov 27, 2015 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$ Commented Nov 27, 2015 at 11:33
  • $\begingroup$ @JacobAkkerboom good point. $\endgroup$
    – Kuba
    Commented Nov 27, 2015 at 11:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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