Skip to content

Commit 94e6bbd

Browse files
author
Chris White
committed
Added initial tests of -s
1 parent b1de243 commit 94e6bbd

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

perlpp.pl

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,8 @@ sub OutputResult {
453453
# --man reserved
454454
# INPUT_FILENAME assigned by parse_command_line_into
455455
OUTPUT_FILENAME => ['o','|output=s', ""],
456-
DEFS => ['D','|define:s%'],
456+
DEFS => ['D','|define:s%'], # In %D, and text substitution
457+
SETS => ['s','|set:s%'], # Extra data in %S, without text substitution
457458
# --usage reserved
458459
# -? reserved
459460
);
@@ -526,7 +527,7 @@ sub Main {
526527

527528
# Transfer parameters from the command line (-D) to the processed file,
528529
# as textual representations of expressions.
529-
# The parameters are in %D at runtime, by analogy with -S and %S.
530+
# The parameters are in %D at runtime.
530531
print "my %D = (\n";
531532
for my $defname (keys %{$opts{DEFS}}) {
532533
my $val = ${$opts{DEFS}}{$defname} // 'true';
@@ -563,6 +564,20 @@ sub Main {
563564
keys %{$opts{DEFS}};
564565
}
565566

567+
# Now do SETS: -s or --set, into %S by analogy with -D and %D.
568+
print "my %S = (\n";
569+
for my $defname (keys %{$opts{SETS}}) {
570+
my $val = ${$opts{SETS}}{$defname};
571+
if(!defined($val)) {
572+
}
573+
$val = 'true' if $val eq '';
574+
# "-s foo" (without a value) sets it to _true_ so
575+
# "if($S{foo})" will work. Getopt::Long gives us '' as the
576+
# value in that situation.
577+
print " $defname => $val,\n";
578+
}
579+
print ");\n";
580+
566581
# Initial code from the command line, if any
567582
print $opts{EVAL}, "\n" if $opts{EVAL};
568583

t/cmdline.t

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ my @testcases=(
6161
['-Dfoo=\"bar\" --define barfoo','_foo foo foobar barfoo',
6262
qr/^_foo bar foobar barfoo$/ ],
6363

64+
# Sets, which do not textually substitute
65+
['-sfoo=42','<? my $foo; ?>foo',qr/^foo$/ ],
66+
['-sfoo=42','<? my $foo; ?><?= $S{foo} ?>',qr/^42$/ ],
67+
6468
# Conditionals
6569
['-Dfoo=42','<?:if foo==2?>yes<?:else?>no<?:endif?>',qr/^no$/ ],
6670
['-Dfoo=2','<?:if foo==2?>yes<?:else?>no<?:endif?>',qr/^yes$/ ],

0 commit comments

Comments
 (0)