Skip to content

Commit c9934c9

Browse files
authored
Merge pull request #21 from cxw42/5a1
v0.3.2: Run on 5.10.1; improve documentation
2 parents b9824fe + 60def76 commit c9934c9

File tree

8 files changed

+453
-239
lines changed

8 files changed

+453
-239
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*~
1212
~*
1313
*.swp
14+
*.tmp
1415

1516
# from GitHub's perl .gitignore:
1617
!Build/
@@ -51,3 +52,6 @@ inc/
5152

5253
# Capture::Tiny
5354
DEBUG*
55+
56+
# App::FatPacker
57+
*.trace

Makefile.PL

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@ WriteMakefile(
3131
'ExtUtils::MakeMaker' => '0',
3232
},
3333
BUILD_REQUIRES => {
34-
'Test::More' => '0',
3534
'App::FatPacker' => '0',
35+
'IPC::Run3' => '0',
36+
'Test::More' => '0',
3637
},
3738
PREREQ_PM => {
38-
'Pod::Usage' => '0',
3939
'Getopt::Long' => '2.50', # Per issue #17
40+
'Pod::Usage' => '0',
4041
},
4142
dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', },
4243
clean => { FILES => 'Text-PerlPP-* fatlib' },

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@ PerlPP runs in two passes: it generates a Perl script from your input, and then
1010
it runs the generated script. If you see `error at (eval ##)`
1111
(for some number `##`), it means there was an error in the generated script.
1212

13+
Installation
14+
------------
15+
16+
Easy way #1: `cpanm Text::PerlPP`
17+
(using [cpanminus](https://metacpan.org/pod/App::cpanminus).
18+
19+
Easy way #2: copy the `perlpp` file from the
20+
[latest release](https://github.com/interpreters/perlpp/releases/latest)
21+
into a directory in your `PATH`, and `chmod a+x perlpp`.
22+
23+
For development or more information, see the other [README](README).
24+
1325
Usage
1426
-----
1527

bin/perlpp

Lines changed: 323 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,326 @@
33
# perl -Ilib bin/perlpp
44
use strict; use warnings; use Text::PerlPP;
55
exit(Text::PerlPP::Main(\@ARGV));
6-
# vi: set ft=perl: #
6+
__END__
7+
# ### Documentation #######################################################
8+
9+
=pod
10+
11+
=encoding UTF-8
12+
13+
=head1 NAME
14+
15+
perlpp - Perl preprocessor: process Perl code within any text file
16+
17+
=head1 USAGE
18+
19+
perlpp [options] [--] [filename]
20+
21+
If no [filename] is given, input will be read from stdin.
22+
23+
Run C<perlpp --help> for a quick reference, or C<perlpp --man> for full docs.
24+
25+
=head1 OPTIONS
26+
27+
=over
28+
29+
=item -o, --output B<filename>
30+
31+
Output to B<filename> instead of STDOUT.
32+
33+
=item -D, --define B<name>[=B<value>]
34+
35+
In the generated script, set C<< $D{B<name>} >> to B<value>.
36+
The hash C<%D> always exists, but is empty if no B<-D> options are
37+
given on the command line.
38+
39+
The B<name> will also be replaced with the B<value> in the text of the file.
40+
If B<value> cannot be evaluated, no substitution is made for B<name>.
41+
42+
If you omit the B<< =value >>, the value will be the constant C<true>
43+
(see L<"The generated script"|/"THE GENERATED SCRIPT">, below), and no text substitution
44+
will be performed.
45+
46+
This also saves the value, or C<undef>, in the generation-time hash
47+
C<< %Text::PerlPP::Defs >>. This can be used, e.g., to select include
48+
filenames depending on B<-D> arguments.
49+
50+
See L<"Definitions"|/"DEFINITIONS">, below, for more information.
51+
52+
=item -e, --eval B<statement>
53+
54+
Evaluate the B<statement> before any other Perl code in the generated
55+
script.
56+
57+
=item -E, --debug (or -d for backwards compatibility)
58+
59+
Don't evaluate Perl code, just write the generated code to STDOUT.
60+
By analogy with the C<-E> option of gcc.
61+
62+
=item -k, --keep-going
63+
64+
Normally, errors in a C<!command> sequence will terminate further
65+
processing. If B<-k> is given, an error message will be printed to stderr,
66+
but the script will keep running.
67+
68+
=item -s, --set B<name>[=B<value>]
69+
70+
As B<-D>, but:
71+
72+
=over
73+
74+
=item *
75+
76+
Does not substitute text in the body of the document;
77+
78+
=item *
79+
80+
Saves into C<< %Text::PerlPP::Sets >> at generation time; and
81+
82+
=item *
83+
84+
Saves into C<< %S >> in the generated script.
85+
86+
=back
87+
88+
=item --man
89+
90+
Full documentation, viewed in your default pager if configured.
91+
92+
=item -h, --help
93+
94+
Usage help, printed to STDOUT.
95+
96+
=item -?, --usage
97+
98+
Shows just the usage summary
99+
100+
=item --version
101+
102+
Show the version number of perlpp
103+
104+
=back
105+
106+
=head1 DEFINITIONS
107+
108+
B<-D> and B<-s> items may be evaluated in any order ---
109+
do not rely on left-to-right
110+
evaluation in the order given on the command line.
111+
112+
If your shell strips quotes, you may need to escape them: B<value> must
113+
be a valid Perl expression. So, under bash, this works:
114+
115+
perlpp -D name=\"Hello, world!\"
116+
117+
The backslashes (C<\"> instead of C<">) are required to prevent bash
118+
from removing the double-quotes. Alternatively, this works:
119+
120+
perlpp -D 'name="Hello, World"'
121+
122+
with the whole argument to B<-D> in single quotes.
123+
124+
Also note that the space after B<-D> is optional, so
125+
126+
perlpp -Dfoo
127+
perlpp -Dbar=42
128+
129+
both work.
130+
131+
=head1 INPUT FORMAT
132+
133+
All text from the input is passed literally to the output unless enclosed
134+
in the delimiters C<< <? >> and C<< ?> >>. This is similar to PHP's rule.
135+
The first character after the opening delimiter selects one of the modes,
136+
and defines the content between the delimiters.
137+
138+
PerlPP first generates a script based on the input ("generation time"), then
139+
evaluates that script ("eval time") to produce the output. All Perl code is
140+
run when the script is evaluated, except for commands notes as occuring at
141+
generation time.
142+
143+
For double-quotes adjacent to the delimiters, add whitespace between
144+
the quote and the delimiter. For example, use C<< <? " >> and C<< " ?> >>
145+
instead of C<< <?" >> and C<< "?> >>. The exception is if you want to invoke
146+
L<capturing|"Capturing">, described below.
147+
148+
=head2 Modes
149+
150+
=over
151+
152+
=item Code mode: C<< <? arbitrary Perl code ?> >>
153+
154+
The content is whatever Perl code you want. It will be executed
155+
at evaluation time. It is up to you to make sure things are properly
156+
nested.
157+
158+
=item Echo mode: C<< <?= Perl expression ?> >>
159+
160+
C<< <?= expr ?> >> is shorthand for C<< <? print expr ; ?> >>.
161+
162+
=item Code mode with newline: C<< <?/ arbitrary Perl code ?> >>
163+
164+
The same as C<< <? ?> >>, but sticks a C<print "\n";> in front of the code
165+
you provide.
166+
167+
=item Comment mode: C<< <?# arbitrary text not including '?>' ?> >>
168+
169+
Everything in a comment is ignored, suprisingly enough!
170+
171+
=item External mode: C<< <?! command [args...] ?> >>
172+
173+
Runs the given string using C<qx//>. If the command fails (returns nonzero),
174+
execution halts unless C<-k> was given. The stdout of the command is
175+
include with the rest of the output of the script.
176+
177+
=item Command mode: C<< <?:command_name [optional args] ?> >>
178+
179+
Runs a PerlPP command.
180+
181+
=back
182+
183+
=head2 PerlPP Commands
184+
185+
=over
186+
187+
=item C<< <?:include filename ?> >>
188+
189+
Include the contents of the file called C<filename> at generation time.
190+
The included file is processed as if its contents occurred inline in the
191+
calling file.
192+
193+
If the filename includes spaces, use double-quotes:
194+
195+
<?:include "some long filename" ?>
196+
197+
Make sure to include the space after the closing double-quote.
198+
199+
=item C<< <?:prefix foo bar ?> >>
200+
201+
After the prefix command, if a word starts with C<foo>, change the C<foo>
202+
to C<bar>.
203+
204+
=item C<< <?:macro some_perl_code ?> >>
205+
206+
Run C<some_perl_code> at the time of script generation. Whatever output
207+
the perl code produces will be included verbatim in the script output.
208+
See L</README.md> for examples.
209+
210+
=item C preprocessor emulation
211+
212+
The following work similarly to the corresponding functions of the
213+
C preprocessor: C<< <?:define NAME ?> >>, C<< <?:undef NAME ?> >>,
214+
C<< <?:ifdef NAME ?> >>, C<< <?:ifndef NAME ?> >>, C<< <?:else ?> >>,
215+
C<< <?:endif ?> >>, C<< <?:if NAME CONDITION ?> >>, and
216+
C<< <?:elsif NAME CONDITION ?> >> (or C<elif> or C<elseif>).
217+
218+
=back
219+
220+
=head2 Capturing
221+
222+
Capturing permits you to express single-quoted strings without having to
223+
quote and escape. For example, C<< <? print "?>some $text "string"<?" ; ?> >>
224+
outputs C<some $text "string"> literally, without substituting C<$text> and
225+
without the need to escape the double-quotes. That way you don't have to
226+
express long blocks of text as Perl string literals.
227+
228+
Capturing can be used anywhere a Perl string expression is valid.
229+
230+
PerlPP commands can be nested within captured strings. For example,
231+
running the script
232+
233+
<?= "!" . "?>foo<?= 42 ?><?" . "bar" ?>
234+
235+
will output C<!foo42bar>. The C<42> is generated by the nested
236+
C<< <?= 42 ?> >> expression.
237+
238+
=head1 THE GENERATED SCRIPT
239+
240+
The code you specify in the input file is in a Perl environment with the
241+
following definitions in place:
242+
243+
package PPP_foo;
244+
use 5.010001;
245+
use strict;
246+
use warnings;
247+
use constant { true => !!1, false => !!0 };
248+
249+
where B<foo> is the input filename, if any, transformed to only include
250+
[A-Za-z0-9_].
251+
252+
This preamble requires Perl 5.10.1, which perlpp itself requires.
253+
On the plus side, requring v5.10.1 gives you C<//>
254+
(the defined-or operator) and the builtin C<say>.
255+
The preamble also keeps you safe from some basic issues.
256+
257+
=head1 BUGS
258+
259+
Please report any bugs or feature requests through GitHub, via
260+
L<https://github.com/interpreters/perlpp/issues>.
261+
262+
=head1 SUPPORT
263+
264+
You can find documentation for this module with the perldoc command.
265+
266+
perldoc Text::PerlPP
267+
268+
You can also look for information at:
269+
270+
=over 4
271+
272+
=item * AnnoCPAN: Annotated CPAN documentation
273+
274+
L<http://annocpan.org/dist/Text-PerlPP>
275+
276+
=item * CPAN Ratings
277+
278+
L<http://cpanratings.perl.org/d/Text-PerlPP>
279+
280+
=item * Search CPAN
281+
282+
L<http://search.cpan.org/dist/Text-PerlPP/>
283+
284+
=back
285+
286+
=head1 ALTERNATIVES
287+
288+
Turns out there are about 2^googol modules that do similar things. We think
289+
this one works pretty nicely, but here are some others in case you disagree.
290+
In no particular order: L<Text::EP3>, L<Text::Template>, L<Basset::Template>,
291+
L<ExtUtils::PerlPP>, L<HTML::EP>, L<PML>, L<Preproc::Tiny>, L<ePerl>, L<iperl>.
292+
293+
=head1 AUTHORS
294+
295+
Andrey Shubin (d-ash at Github; L<[email protected]>) and
296+
Chris White (cxw42 at Github; L<[email protected]>).
297+
298+
=head1 LICENSE AND COPYRIGHT
299+
300+
Copyright 2013-2018 Andrey Shubin and Christopher White.
301+
302+
This program is distributed under the MIT (X11) License:
303+
L<http://www.opensource.org/licenses/mit-license.php>
304+
305+
Permission is hereby granted, free of charge, to any person
306+
obtaining a copy of this software and associated documentation
307+
files (the "Software"), to deal in the Software without
308+
restriction, including without limitation the rights to use,
309+
copy, modify, merge, publish, distribute, sublicense, and/or sell
310+
copies of the Software, and to permit persons to whom the
311+
Software is furnished to do so, subject to the following
312+
conditions:
313+
314+
The above copyright notice and this permission notice shall be
315+
included in all copies or substantial portions of the Software.
316+
317+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
318+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
319+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
320+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
321+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
322+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
323+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
324+
OTHER DEALINGS IN THE SOFTWARE.
325+
326+
=cut
327+
328+
# vi: set ft=perl ts=4 sts=0 sw=4 noet ai: #

0 commit comments

Comments
 (0)