@@ -56,7 +56,7 @@ use constant OB_CONTENTS => 1;
56
56
use constant OB_STARTLINE => 2;
57
57
58
58
# What $self is called inside a script package
59
- use constant PPP_SELF_INSIDE => ' _PerlPP_Instance ' ;
59
+ use constant PPP_SELF_INSIDE => ' PSelf ' ;
60
60
61
61
# Debugging info
62
62
my @OBModeNames = qw( plain capture code echo command comment) ;
@@ -239,11 +239,58 @@ sub ExecuteCommand {
239
239
240
240
} elsif ( $cmd =~ / ^macro\s ++(.*+)$ /si ) {
241
241
$self -> StartOB(); # plain text
242
- eval ( $1 ); warn $@ if $@ ;
242
+
243
+ # Create the execution environment for the macro:
244
+ # - Run in the script's package. Without `package`, the eval'ed
245
+ # code runs in Text::PerlPP.
246
+ # - Make $PSelf available with `our`. Each `eval` gets its own
247
+ # set of lexical variables, so $PSelf would have to be referred
248
+ # to with its full package name if we didn't have the `our`.
249
+ # TODO add a pound line to this eval based on the current line number
250
+
251
+ my $code = qq{ ;
252
+ package $self ->{Package} {
253
+ our \$ @{[PPP_SELF_INSIDE]};
254
+ $1
255
+ };
256
+ } ;
257
+
258
+ print " Macro code run:\n $code \n " =~ s / ^/ #/ gmr
259
+ if ($self -> {Opts }-> {DEBUG });
260
+ eval $code ;
261
+ my $err = $@ ; chomp $err ;
243
262
emit ' print ' . $self -> PrepareString( $self -> EndOB() ) . " ;\n " ;
244
263
264
+ # Report the error, if any. Under -E, it's a warning.
265
+ my $errmsg = " Error: $err \n in immediate " . substr ($1 , 0, 40) . ' ...' ;
266
+ if ($self -> {Opts }-> {DEBUG }) {
267
+ warn $errmsg if $err ;
268
+ } else {
269
+ die $errmsg if $err ;
270
+ }
271
+
245
272
} elsif ( $cmd =~ / ^immediate\s ++(.*+)$ /si ) {
246
- eval ( $1 ); warn $@ if $@ ;
273
+ # TODO refactor common code between macro and immediate
274
+
275
+ # TODO add a pound line to this eval
276
+ my $code = qq{ ;
277
+ package $self ->{Package} {
278
+ our \$ @{[PPP_SELF_INSIDE]};
279
+ $1
280
+ };
281
+ } ;
282
+ print " Immediate code run:\n $code \n " =~ s / ^/ #/ gmr
283
+ if ($self -> {Opts }-> {DEBUG });
284
+ eval ( $code );
285
+ my $err = $@ ; chomp $err ;
286
+
287
+ # Report the error, if any. Under -E, it's a warning.
288
+ my $errmsg = " Error: $err \n in immediate " . substr ($1 , 0, 40) . ' ...' ;
289
+ if ($self -> {Opts }-> {DEBUG }) {
290
+ warn $errmsg if $err ;
291
+ } else {
292
+ die $errmsg if $err ;
293
+ }
247
294
248
295
} elsif ( $cmd =~ / ^prefix\s ++(\S ++)\s ++(\S ++)\s *+$ /i ) {
249
296
$self -> {Prefixes }-> { $1 } = $2 ;
@@ -773,7 +820,16 @@ sub Main {
773
820
$self -> {Package } =~ s / ^.*?([a-z_][a-z_0-9.]*).pl?$/ $1 / i ;
774
821
$self -> {Package } =~ s / [^a-z0-9_]/ _/ gi ;
775
822
# $self->{Package} is not the whole name, so can start with a number.
776
- $self -> {Package } .= $#Instances ;
823
+ $self -> {Package } = " PPP_$self ->{Package}$#Instances " ;
824
+
825
+ # Make $self accessible from inside the package.
826
+ # This has to happen first so that :macro or :immediate blocks in the
827
+ # script can access it while the input is being parsed.
828
+ {
829
+ no strict ' refs' ;
830
+ ${ " $self ->{Package}::" . PPP_SELF_INSIDE }
831
+ = $Text::PerlPP::Instances [$#Instances ];
832
+ }
777
833
778
834
$self -> StartOB(); # Output from here on will be included in the generated script
779
835
@@ -782,12 +838,9 @@ sub Main {
782
838
$self -> emit_pound_line( ' <package header>' , 1 );
783
839
784
840
# Open the package
785
- emit " package PPP_ $self ->{Package};\n use 5.010001;\n use strict;\n use warnings;\n " ;
841
+ emit " package $self ->{Package};\n use 5.010001;\n use strict;\n use warnings;\n " ;
786
842
emit " use constant { true => !!1, false => !!0 };\n " ;
787
-
788
- # Make $self accessible from inside the package.
789
- emit ' my $' . PPP_SELF_INSIDE .
790
- ' = $Text::PerlPP::Instances[' . $#Instances . " ];\n " ;
843
+ emit ' our $' . PPP_SELF_INSIDE . " ;\n " ; # Lexical alias for $self
791
844
792
845
# Definitions
793
846
@@ -873,7 +926,7 @@ sub Main {
873
926
874
927
} else {
875
928
$self -> StartOB(); # Start collecting the output of the Perl script
876
- my $result ; # To save any errors from the eval
929
+ my $result ; # To save any errors from the eval
877
930
878
931
# TODO hide %Defs and others of our variables we don't want
879
932
# $script to access.
0 commit comments