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:
There is an ordering on the attributes, so that (for example) 'onInit' always happens before 'onAwaken'.<script onInit='our $x;' onAwaken='print "X is now ", ++$x, "\n"' />
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:
then the sub{ } wrapping limits the scope of each attribute and prevents the 'use strict' in 'onInit' from reaching the code in 'onAwaken'.<script onInit='use strict; my $x;' onAwaken='print ++$xx' />
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:
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.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; }
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |