How can I get multiple pieces of a script compiled in the same lexical scope, but still get accurate errors and warnings?

I have a system that is configured with an XML file. Think of it as a web browser running on an HTML page; the analogy is very close. One of the allowed tags is <script>, which has about a dozen different attributes that can be set to perl snippets, and they will be run at the appropriate time in the lifecycle of the script. An example:

<script onInit='our $x;' onAwaken='print "X is now ", ++$x, "\n"' />
There is an ordering on the attributes, so that (for example) 'onInit' always happens before 'onAwaken'.

The application does realtime interactive graphics, so speed is a major concern. Originally, I was caching the compiled form of the scripts by wrapping them in "sub { ... }" and eval'ing them, then saving away the resulting code ref to be invoked at the appropriate time. (This is all done in XS, but I don't think that will matter for the purposes of this question.)

The problem is that I would really like to be able to use strict. But if I have the following XML:

<script onInit='use strict; my $x;' onAwaken='print ++$xx' />
then the sub{ } wrapping limits the scope of each attribute and prevents the 'use strict' in 'onInit' from reaching the code in 'onAwaken'.

So now I'm trying to switch to a somewhat hacky setup where instead of individually compiling each of the attributes, I instead merge them together into one big script that looks like:

sub { goto __ON_INIT if $_[0] == 1; goto __ON_AWAKEN if $_[0] == 2; ... __ON_INIT: use strict; my $x; return; __ON_AWAKEN: print ++$x; return; }
This works better for the most part, but introduces its own set of annoyances. If one of the attributes contains a syntax error, then the error will be emitted when the entire thing is compiled, and I won't know which attribute to blame. (I can't use #line directives to distinguish, because I'm already using them to map back to the original XML file, and two attributes could easily be on the same line.) Also, when any attribute changes, I have to recompile the entire thing over again. I'd prefer not to spend the time, but that's probably not bad enough to worry about. Also, I don't see any way to avoid it if I want everything in the same lexical scope. A much larger concern is if compiling one of the (unchanged) attributes produces a warning message -- I will now see the warning message when a different attribute changes.

Is there some obvious approach that I am overlooking?

I guess I want a continuation-based Perl compiler. Heh.


In reply to Concatenating scripts intelligently by sfink

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.