in reply to howto parse (or determining end) of a line of perl

I think I remember LanX talking about a REPL of his where a new line would be prompted and concatenated to the current statement until it did compile. To check for compilation without actually executing anything you can do something like: eval "sub DUMMY { $code }; 1" or die "Failed! $@". Any use statment or BEGIN block will still be executed though.

Of course this requires a way to cancel the current input, maybe with an EOF token in Linux, or anything that you are not going to put in actual perl code.

Edit: rephrased the first sentence for clarity.

Replies are listed 'Best First'.
Re^2: howto parse (or determining end) of a line of perl
by perl-diddler (Chaplain) on Aug 26, 2016 at 00:28 UTC
    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'....;-)

      don't really need the sub on the outside as it doesn't really provide any extra protection that I know of

      It prevents the code from being executed. Except, of course, for BEGIN blocks and use statements (which also execute when you use the -c (compile only) option to perl).