eval "sub DUMMY { $code }; 1" ..." -- that does something if "$code" contains a sub, but if you are willing to allow for "less than perfect" (fine for most personal toys), then yeah... don't really need the sub on the outside as it doesn't really provide any extra protection that I know of.

Another layer of "less than perfect" protection, but w/the advantage of getting a 'default' (overridable) namespace protection, is to put the evaluated code in a separate package namespace -- so all my calculator's namespace evals are done in a 'USER' namespace -- so user routines and constants default to be separated from my machinery namespace (the keyword in many of these solutions being 'default' to allow for simple, DWIM behavior).

I think there might be some better sandboxing mods in CPAN, if you wanted to get paranoid, but might not be worth the overhead/side effects for a personal 'toy'....;-)


In reply to Re^2: howto parse (or determining end) of a line of perl by perl-diddler
in thread howto parse (or determining end) of a line of perl by perl-diddler

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.