Skip to content

Commit 193cffa

Browse files
author
Chris White
committed
Added %Sets at generation time; updated docs.
1 parent 94e6bbd commit 193cffa

File tree

4 files changed

+66
-10
lines changed

4 files changed

+66
-10
lines changed

README.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,20 @@ Usage
1616
Usage: perl perlpp.pl [options] [filename]
1717
Options:
1818
-o, --output filename Output to the file instead of STDOUT.
19-
-D, --define name=value Set $D{name}=value in the generated code.
20-
The hash %D always exists, but is empty
21-
if you haven't specified any -D options.
22-
-e, --eval statement Evaluate the statement(s) before any Perl code.
23-
-E, --debug Don't evaluate Perl code, just write it to STDERR.
19+
-D, --define name=value Set $D{name}=value in the generated
20+
code. The hash %D always exists, but
21+
is empty if you haven't specified any
22+
-D options.
23+
Also substitutes _name_ with _value_
24+
in the output file.
25+
_value_ is optional and defaults to
26+
true.
27+
-e, --eval statement Evaluate the statement(s) before any
28+
Perl code of the input files.
29+
-E, --debug Don't evaluate Perl code, just write
30+
it to STDERR.
31+
-s, --set name=value As -D, but gneerates into %S and does
32+
not substitute in the text body.
2433
-h, --help Usage help.
2534

2635
In a **-D** command, the `value` must be a valid Perl value, e.g., `"foo"`
@@ -282,3 +291,9 @@ and create corresponding *~/.vim/after/syntax/FILETYPE.vim*
282291

283292
FILETYPE can be determined with `:set ft?`
284293

294+
## Copyright
295+
296+
Distributed under the MIT license --- see
297+
[LICENSE.txt](LICENSE.txt) for details.
298+
By Andrey Shubin (d-ash at Github) and Chris White (cxw42 at Github).
299+

perlpp.pl

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ package PerlPP;
6969
my $Defs_RE = false; # Regex that matches any -D name
7070
my %Defs_repl_text = (); # Replacement text for -D names
7171

72+
# -s definitions.
73+
my %Sets = (); # Command-line -s arguments
74+
7275
# Output-buffer stack
7376
my @OutputBuffers = (); # each entry is a two-element array
7477

@@ -565,6 +568,15 @@ sub Main {
565568
}
566569

567570
# Now do SETS: -s or --set, into %S by analogy with -D and %D.
571+
572+
# Save a copy for use at generation time
573+
%Sets = map { my $v = eval(${$opts{SETS}}{$_});
574+
warn "Could not evaluate -s \"$_\": $@" if $@;
575+
$_ => ($v // true)
576+
}
577+
keys %{$opts{SETS}};
578+
579+
# Make the copy for runtime
568580
print "my %S = (\n";
569581
for my $defname (keys %{$opts{SETS}}) {
570582
my $val = ${$opts{SETS}}{$defname};
@@ -636,14 +648,18 @@ =head1 OPTIONS
636648
637649
Output to B<filename> instead of STDOUT.
638650
639-
=item -D, --define B<name>=B<value>
651+
=item -D, --define B<name>[=B<value>]
640652
641653
In the generated script, set C<< $D{B<name>} >> to B<value>.
642654
The hash C<%D> always exists, but is empty if no B<-D> options are
643655
given on the command line.
644656
657+
The B<name> will also be replaced with the B<value> in the text of the file.
658+
If B<value> cannot be evaluated, no substitution is made for B<name>.
659+
645660
If you omit the B<< =value >>, the value will be the constant C<true>
646-
(see L</"The generated script">, below).
661+
(see L</"The generated script">, below), and no text substitution
662+
will be performed.
647663
648664
This also saves the value, or C<undef>, in the generation-time
649665
hash C<< %Defs >>. This can be used, e.g., to select include filenames
@@ -661,6 +677,26 @@ =head1 OPTIONS
661677
Don't evaluate Perl code, just write the generated code to STDOUT.
662678
By analogy with the C<-E> option of gcc.
663679
680+
=item -s, --set B<name>[=B<value>]
681+
682+
As B<-D>, but:
683+
684+
=over
685+
686+
=item *
687+
688+
Does not substitute text in the body of the document;
689+
690+
=item *
691+
692+
Saves into C<< %Sets >> at generation time; and
693+
694+
=item *
695+
696+
Saves into C<< %S >> in the generated script.
697+
698+
=back
699+
664700
=item --man
665701
666702
Full documentation, viewed in your default pager if configured.
@@ -677,7 +713,8 @@ =head1 OPTIONS
677713
678714
=head1 DEFINITIONS
679715
680-
B<-D> items may be evaluated in any order --- do not rely on left-to-right
716+
B<-D> and B<-s> items may be evaluated in any order ---
717+
do not rely on left-to-right
681718
evaluation in the order given on the command line.
682719
683720
If your shell strips quotes, you may need to escape them: B<value> must
@@ -722,8 +759,8 @@ =head1 COPYRIGHT
722759
723760
Code at L<https://github.com/d-ash/perlpp>.
724761
Distributed under MIT license.
725-
By Andrey Shubin (L<[email protected]>); additional contributions by
726-
Chris White (cxw42 at Github).
762+
By Andrey Shubin (d-ash at Github; L<[email protected]>) and
763+
Chris White (cxw42 at Github; L<[email protected]>).
727764
728765
=cut
729766

t/cmdline.t

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ my @testcases=(
6464
# Sets, which do not textually substitute
6565
['-sfoo=42','<? my $foo; ?>foo',qr/^foo$/ ],
6666
['-sfoo=42','<? my $foo; ?><?= $S{foo} ?>',qr/^42$/ ],
67+
['--set foo=42','<? my $foo; ?>foo',qr/^foo$/ ],
68+
['--set foo=42','<? my $foo; ?><?= $S{foo} ?>',qr/^42$/ ],
6769

6870
# Conditionals
6971
['-Dfoo=42','<?:if foo==2?>yes<?:else?>no<?:endif?>',qr/^no$/ ],

t/macro.t

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ my @testcases=(
1818
['-D foo=42', '<?:macro say $Defs{foo}; ?>', qr/^42/],
1919
['-D incfile=' . $incfn , '<?:macro Include $Defs{incfile}; ?>',
2020
qr/^a4b/],
21+
['-s incfile=' . $incfn , '<?:macro Include $Sets{incfile}; ?>',
22+
qr/^a4b/],
2123
['', '<?:immediate say "print 128;"; ?>',qr/^128$/],
2224

2325
); #@testcases

0 commit comments

Comments
 (0)