in reply to Concatenating scripts intelligently

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:

use strict; use warnings; package Script; # Constants. my @events = qw( preRestore onRestore onAttach onInit onAwaken onChange ); # Ordered. my %events = map { $events[$_] => $_ } 0..$#events; sub new { my ($class, $snippets) = @_; my @event_line_nums; my $code = "sub {\n"; $code .= " my \$event_num = shift;\n"; $code .= " goto __$events[$_] if \$event_num == $_;\n" foreach 0 .. $#events; foreach my $event (@events) { push @event_line_nums, (() = $code =~ /\n/g) + 1; if (defined $snippets->{$event}) { $code .= "__$event: 1;\n$snippets->{$event}; return;\n"; } else { $code .= "__$event: return;\n"; } } $code .= "\n}\n"; my $sub = eval $code; if ($@) { my ($error_msg, $line_num) = $@ =~ /^(.*) at \(eval \d+\) line (\d+)/s; my $error_event; foreach my $event_num (0..$#events) { last if $line_num < $event_line_nums[$event_num]; $error_event = $event_num; } die("$error_msg in $events[$error_event].\n"); } return bless($sub, $class); } sub _dispatch { my $sub = shift; my $event_num = shift; $rv = eval { $sub->($event_num, @_) }; if ($@) { my ($error_msg) = $@ =~ /^(.*) at \(eval \d+\) line /s; die("$error_msg in $events[$event_num].\n"); } return $rv; } eval "sub $_ { shift->_dispatch($events{$_}, \@_); }" foreach @events; 1;

Update:

Replies are listed 'Best First'.
Re^2: Concatenating scripts intelligently
by sfink (Deacon) on May 10, 2006 at 00:50 UTC
    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.