5
5
# http://darkness.codefu.org/wordpress/2003/03/perl-scoping/
6
6
7
7
package PerlPP ;
8
- our $VERSION = ' 0.2.0 ' ;
8
+ our $VERSION = ' 0.3.0-alpha ' ;
9
9
10
10
use v5.10; # provides // - http://perldoc.perl.org/perl5100delta.html
11
11
use strict;
@@ -63,7 +63,11 @@ package PerlPP;
63
63
my @Preprocessors = ();
64
64
my @Postprocessors = ();
65
65
my %Prefixes = (); # set by ExecuteCommand; used by PrepareString
66
+
67
+ # -D definitions. -Dfoo creates $Defs{foo}==true and $Defs_repl_text{foo}==''.
66
68
my %Defs = (); # Command-line -D arguments
69
+ my $Defs_RE = false; # Regex that matches any -D name
70
+ my %Defs_repl_text = (); # Replacement text for -D names
67
71
68
72
# Output-buffer stack
69
73
my @OutputBuffers = (); # each entry is a two-element array
@@ -156,9 +160,16 @@ sub PrepareString {
156
160
my $s = shift ;
157
161
my $pref ;
158
162
163
+ # Replace -D options. Do this before prefixes so that we don't create
164
+ # prefix matches. TODO? combine the defs and prefixes into one RE?
165
+ $s =~ s / $Defs_RE/ $Defs_repl_text {$1 }/ g if $Defs_RE ;
166
+
167
+ # Replace prefixes
159
168
foreach $pref ( keys %Prefixes ) {
160
169
$s =~ s / (^|\W )\Q $pref\E / $1 $Prefixes { $pref }/ g ;
161
170
}
171
+
172
+ # Quote it for printing
162
173
return QuoteString( $s );
163
174
}
164
175
@@ -513,8 +524,9 @@ sub Main {
513
524
514
525
# Definitions
515
526
516
- # Transfer parameters from the command line (-D) to the processed file.
517
- # The parameters are in %D, by analogy with -D.
527
+ # Transfer parameters from the command line (-D) to the processed file,
528
+ # as textual representations of expressions.
529
+ # The parameters are in %D at runtime, by analogy with -S and %S.
518
530
print " my %D = (\n " ;
519
531
for my $defname (keys %{$opts {DEFS }}) {
520
532
my $val = ${$opts {DEFS }}{$defname } // ' true' ;
@@ -530,9 +542,26 @@ sub Main {
530
542
# Save a copy for use at generation time
531
543
%Defs = map { my $v = eval (${$opts {DEFS }}{$_ });
532
544
warn " Could not evaluate -D \" $_ \" : $@ " if $@ ;
533
- $_ => $v
545
+ $_ => ($v // true)
546
+ }
547
+ keys %{$opts {DEFS }};
548
+
549
+ # Set up regex for text substitution of Defs.
550
+ # Modified from http://www.perlmonks.org/?node_id=989740 by
551
+ # AnomalousMonk, http://www.perlmonks.org/?node_id=634253
552
+ if (%{$opts {DEFS }}) {
553
+ my $rx_search =
554
+ ' \b(' . (join ' |' , map quotemeta , keys %{$opts {DEFS }}) . ' )\b' ;
555
+ $Defs_RE = qr {$rx_search } ;
556
+
557
+ # Save the replacement values. If a value cannot be evaluated,
558
+ # use the name so the replacement will not change the text.
559
+ %Defs_repl_text =
560
+ map { my $v = eval (${$opts {DEFS }}{$_ });
561
+ ($@ || !defined ($v )) ? ($_ => $_ ) : ($_ => (' ' . $v ))
534
562
}
535
563
keys %{$opts {DEFS }};
564
+ }
536
565
537
566
# Initial code from the command line, if any
538
567
print $opts {EVAL }, " \n " if $opts {EVAL };
@@ -549,6 +578,9 @@ sub Main {
549
578
} else {
550
579
StartOB(); # output of the Perl script
551
580
my $result ; # save any errors from the eval
581
+
582
+ # TODO hide %Defs and others of our variables we don't want
583
+ # $script to access.
552
584
eval ( $script ); $result =$@ ;
553
585
554
586
if ($result ) { # Report errors to console and shell
@@ -680,5 +712,5 @@ =head1 COPYRIGHT
680
712
681
713
=cut
682
714
683
- # vi: set ts=4 sts=0 sw=4 noet ai: #
715
+ # vi: set ts=4 sts=0 sw=4 noet ai fo-=o : #
684
716
0 commit comments