That is true. I can see no good way to avoid that, either. Something like:
my $coderef = eval "BEGIN{goto skip;}sub { $code } BEGIN{skip: print ' +Skipped!'}";
will not work. Not that I am sure it would be good if it did either... return and exit both also yield unsatisfying results, not surprisingly.

However, if you are afraid that someone might execute harmful code, then that is a whole nother issue, and you will not be protected just by verifying the syntax (which was what I addressed). However, if you use Safe, and do like this:

use Safe; my $compartment = new Safe; my $coderef = $compartment->reval("sub{$code}") or die "$@"; $coderef->(); # execute the code
The Safe compartment will stop whatever you decide is harmful code even in BEGIN blocks.

If the issue rather is that it is annoying that code will become executed, well then some kind of filter might be in place I guess. For instance, for nearly all applications I could think of (short of a perl editor of course) disallowing the word "BEGIN" in the input (possibly in special cases, like followed by braces) would not be a big problem, would it?

It is definetely something to be aware of though. Good call. :)


You have moved into a dark place.
It is pitch black. You are likely to be eaten by a grue.

In reply to Re: Re: Re: Re: Can you check the syntax of a eval() before executing it? by Dog and Pony
in thread Can you check the syntax of a eval() before executing it? by slloyd

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.