sfink has asked for the wisdom of the Perl Monks concerning the following question:

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.

Replies are listed 'Best First'.
Re: Concatenating scripts intelligently
by ikegami (Patriarch) on May 09, 2006 at 23:35 UTC
    So you want something like
    use Script (); # Data extracted from XML. my %snippets = ( onInit => 'use strict; my $x=2;', onAwaken => 'print ++$y;', ); my $script = Script->new(\%snippets); $script->onInit(); $script->onAwaken();
    which outputs
    Global symbol "$y" requires explicit package name in onAwaken.

    If so, use the following module:

    Update:

    • Made it easier to dispatch by creating the Script class.
    • Handlers can now receive arguments.
    • Handlers can now return a value.
      Yes, I think that does exactly what I want! Thanks for really diving into it!

      I actually want it to output something like

      Global symbol "$y" requires explicit package name in onAwaken snippet at somefile.xml line 42.
      
      ...but that requires external information (the file, line of each attribute) that I didn't mention before, and it's a straightforward addition to your script.

        I figured you'd want something like that. You'll probably want to handle warnings too. local $SIG{__WARN__} will help you do that.

        An improvement I see is to optimize the line count to only count from the point where last count left off.

Re: Concatenating scripts intelligently
by ikegami (Patriarch) on May 09, 2006 at 21:47 UTC

    ( For the reason I explained here, this node doesn't help the OP. This one does. )

    Would the following do the trick?

    # Info extracted from # <script onInit='use strict; my $x; my $y;' # onAwaken='print ++$x' # onAwaken='print ++$y'/> # my $onInit = 'use strict; my $x; my $y;'; my $onAwaken = 'print ++$x'; my $onSleep = 'print ++$y'; # Compile code my $code = "package Sandbox;" $code .= "$onInit;"; $code .= "sub onAwaken { $onAwaken }"; $code .= "sub onSleep { $onSleep }"; eval $code; # Dispatch an event Sandbox->onAwaken() if Sandbox->can('onAwaken');

    The above can easily be expanded to handle multiple documents by generating incremental package names.

      That's very close to just having a separate "declare" section. The OP can do that too.

      ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Re: Concatenating scripts intelligently
by salva (Canon) on May 09, 2006 at 21:51 UTC
    I think this would work...
    my $init = eval <<EOI; sub { $INIT_CODE; my \$sub = eval q(sub { $AWAKEN_CODE }); die "bad onAwaken: \$@" if \$@; \$sub; } EOI die "bad onInit: $@" if $@; my $awaken = $init->(); ... # and later onAwaken... $awaken->();
    though you would probably want to improve the quoting for the inner eval argument.

    update: or if several attributes as onAwaken can exist:

    my $init = eval <<EOI; sub { $INIT_CODE; my \$sub = eval 'sub { \$_[1] }'; die "bad \$_[0] code: \$@" if \$@; \$sub; } EOI die "bad onInit: $@" if $@; my $awaken = $init->(onAwaken =>$AWAKEN_CODE); my $foo = $init->(onFoo => $FOO_CODE); ... # and later onAwaken... $awaken->(); # and onFoo; $foo->();
Re: Concatenating scripts intelligently
by sfink (Deacon) on May 09, 2006 at 22:43 UTC
    Both solutions posted so far rely on having onInit be the one declaration section, but I actually have 11 different attributes, and at least four of them would make sense as declaration sections. They're also ordered among themselves. Here's a still incomplete but perhaps more representative list of attributes and their ordering:
    • preRestore
    • onRestore
    • onAttach
    • onInit
    • onAwaken
    • onChange
    Oh, and I like the idea of sandboxing everything into a package -- but I'm already doing that.

    Update: Just to be explicit, here's an example:

    <script preRestore='use strict' onRestore='my $pos = [ 10, 20 ]' onInit='my ($x, $y) = @$pos;' onChange='$x += 1; $y += 2; $pos = [ $x, $y ]' />
      Both solutions posted so far rely on having onInit be the one declaration section,

      Not at all. Concatenate them! ( Oh I see, they're suppose to be called at different times. That means while the compilation of onRestore's snippet has to be done at the same time as the compilation preRestore's snippet's, their execution occur at different times. Truly my solution does not do that. )

      Oh, and I like the idea of sandboxing everything into a package -- but I'm already doing that

      Despite the name, my code isn't sandboxed. It can freely modify other packages. You'd probably have to use Safe to truly sandbox some code.

        Sorry, I was using "sandboxing" loosely. It's still useful, especially since when I delete one of these script tag nodes, I can use Symbol.pm to delete its package and get rid of most of what it did in the (global) perl interpreter. If the script puts anything into another package, then it will survive indefinitely -- but that is usually more of a feature than a drawback, since it can be convenient to write a script that, say, rewrites the menu system permanently when you run it. I'd rather give script writers loaded bazookas and maps showing where their feet are, than try to enforce limitations externally.

        Having a unique package is good for other things too, but I don't suppose it's relevant to this thread.