@@ -4,23 +4,45 @@ PerlPP: Perl preprocessor
44Translates ** Text+Perl** to ** Text** .
55It can be used for any kind of text templating, e.g. code generation.
66No external modules are required, just a single file.
7+ Requires Perl 5.10+.
78
8- Usage: perl perlpp.pl [options] [filename]
9- Options:
10- -o, --output filename Output to the file instead of STDOUT.
11- -s, --set name=value Set $S{name}=value in the generated code.
12- The hash %S always exists, but is empty
13- if you haven't specified any -s options.
14- -e, --eval expression Evaluate the expression(s) before any Perl code.
15- -d, --debug Don't evaluate Perl code, just write it to STDERR.
16- -h, --help Usage help.
9+ PerlPP runs in two passes: it generates a Perl script from your input, and then
10+ it runs the generated script. If you see ` error at (eval ##) `
11+ (for some number ` ## ` ), it means there was an error in the generated script.
1712
18- Syntax
19- ------
13+ Usage
14+ -----
2015
21- Syntax is a bit similar to PHP.
22- Perl code has to be included between ` <? ` and ` ?> ` tags.
23- There are several modes, indicated by the opening tag:
16+ Usage: perl perlpp.pl [options] [filename]
17+ Options:
18+ -o, --output filename Output to the file instead of STDOUT.
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.
33+ -h, --help Usage help.
34+
35+ In a ** -D** command, the ` value ` must be a valid Perl value, e.g., ` "foo" `
36+ for a string. This may require you to escape quotes in the ** -D** argument,
37+ depending on your shell. E.g., if ` -D foo="bar" ` doesn't work, try
38+ ` -D 'foo="bar"' ` (with single quotes around the whole ` name=value ` part).
39+
40+ Syntax of the input file
41+ ------------------------
42+
43+ The syntax is a bit similar to PHP.
44+ Perl code is included between ` <? ` and ` ?> ` tags.
45+ There are several modes, indicated by the character after the ` <? ` :
2446
2547 <? code mode: Perl code is between the tags.
2648 <?= echo mode: prints a Perl expression
@@ -41,6 +63,19 @@ produces the result:
4163
4264 <?x this tag is passed as is ?> because "x" is not a valid mode
4365
66+ The Generated Script
67+ --------------------
68+
69+ The generated script:
70+
71+ - is in its own package, named based on the input filename
72+ - ` use ` s ` 5.010 ` , ` strict ` , and ` warnings `
73+ - provides constants ` true ` (=` !!1 ` ) and ` false ` (=` !!0 ` ) (with ` use constant ` )
74+ - Declares ` my %D ` and initializes ` %D ` based on any ** -D** options you provide
75+
76+ Other than that, everything in the script comes from your input file(s).
77+ Use the ** -E** option to see the generated script.
78+
4479Examples
4580--------
4681
@@ -108,27 +143,43 @@ So `<?/ ... ?>` is effectively a shorthand for `<? print "\n"; ... ?>`.
108143Commands
109144--------
110145
146+ ### Include
147+
111148 <?:include file.p ?>
149+ <?:include "long file name.p" ?>
112150
113- or ` <?:include "long file name.p" ?> ` (keep a whitespace between ` " ` and ` ?> ` , explained further).
114151Includes source code of another PerlPP file into this position.
115152Note that this file can be any PerlPP input, so you can also use this to
116153include plain text files or other literal files.
154+ When using the long form, make sure there is whitespace between the trailing
155+ ` " ` and the closing tag ` ?> ` , as explained below under "Capturing."
156+
157+ ### Prefix
117158
118159 <?:prefix foo bar ?>
119160
120161Replaces word prefixes in the following output.
121162In this case words like ` fooSomeWord ` will become ` barSomeWord ` .
122163
164+ ### Macro
165+
123166 <?:macro some_perl_code; ?>
124167
125168will run ` some_perl_code; ` at the time of script generation. Whatever output
126169the perl code produces will be included verbatim in the script output.
127170This can be used to dynamically select which files you want to include,
128- using
171+ using the provided ` Include() ` function. For example:
129172
130173 <?:macro my $fn="some_name"; Include $fn; ?>
131174
175+ has the same effect as
176+
177+ <?:include some_name ?>
178+
179+ but ` $fn ` can be determined programmatically. Note that it is not currently
180+ possible to select the filename to ` Include ` based on defines set with ** -D** ,
181+ since those do not take effect until the script has been generated.
182+
132183Capturing
133184---------
134185
@@ -137,16 +188,16 @@ Sometimes it is great to get (capture) source text into a Perl string.
137188 "?> start of capturing
138189 <?" end of capturing
139190
140- For example
191+ There must be no whitespace between the ` " ` and the ` ?> ` or ` <? ` . For example:
141192
142193 <? print "?>That's cool<?" . "?>, really.<?"; ?>
143194
144195is the same as
145196
146197 <? print 'That\'s cool' . ', really.'; ?>
147198
148- Captured strings are properly escaped, and can be sequenced like in this example.
149- Moreover, they can be nested!
199+ Captured strings are properly escaped, and can be sequenced like in
200+ this example. Moreover, they can be nested!
150201
151202 <?
152203 sub ABC {
@@ -160,30 +211,77 @@ Moreover, they can be nested!
160211 <? ABC(); ?>
161212 <?" ); ?>
162213
163- Printed characters from the second ` ABC() ` call are attached to the string ` 'alphabet ' ` ,
164- so the result will be
214+ Printed characters from the second ` ABC() ` call are attached to the
215+ string ` 'alphabet ' ` , so the result will be
165216
166217 abcdefghijklmnopqrstuvwxyz
167218 ALPHABET
168219 ABCDEFGHIJKLMNOPQRSTUVWXYZ
169220
170221Capturing works in all modes: code, echo, or command mode.
171222
172- Custom Preprocessors
173- --------------------
223+ C Preprocessor Emulation
224+ ------------------------
174225
175- It's possible to create your own pre/post-processors with ` PerlPP::AddPreprocessor ` and ` PerlPP::AddPostprocessor ` .
176- This feature is used in [ BigBenBox] ( https://github.com/d-ash/BigBenBox ) for generating code in the C programming language.
226+ The ** -D** switch defines elements of ` %D ` . If you do not specify a
227+ value, the value ` true ` (a constant in the generated script) will be used.
228+ The following commands work mostly analogously to their C preprocessor
229+ counterparts.
177230
178- Future
179- ------
231+ - ` <?:define NAME ?> `
232+ - ` <?:undef NAME ?> `
233+ - ` <?:ifdef NAME ?> `
234+ - ` <?:else ?> `
235+ - ` <?:endif ?> `
236+ - ` <?:if NAME CONDITION ?> `
237+ - ` <?:elsif NAME CONDITION ?> ` (` elif ` and ` elseif ` are synonyms)
238+
239+ For example:
240+
241+ <?:ifdef NAME ?>
242+ foo
243+ <?:endif ?>
244+
245+ is the same as the more verbose script:
246+
247+ <? if(defined($D{NAME})) { ?>
248+ foo
249+ <? } ?>
250+
251+ ### If and Elsif
252+
253+ Tests with ` <?:if NAME ... ?> ` and ` <?:elsif NAME ... ?> ` have two restrictions:
254+
255+ - If ` $D{NAME} ` does not exist, the test will be ` false ` regardless
256+ of the condition `...`.
257+ - The ` ... ` must be a valid Perl expression when
258+ `$D{NAME}` is added to the beginning, with no
259+ parentheses around it.
260+
261+ For example, ` <?:if FOO eq "something" ?> ` (note the whitespace before ` ?> ` !)
262+ will work fine. However, if you want to test ` (FOO+1)*3 ` , you will need
263+ to use the full Perl code.
264+
265+ Other Features
266+ --------------
267+
268+ ### Custom Preprocessors
269+
270+ It's possible to create your own pre/post-processors in a ` <?:macro ?> ` block
271+ using ` PerlPP::AddPreprocessor ` and ` PerlPP::AddPostprocessor ` .
272+ This feature is used in [ BigBenBox] ( https://github.com/d-ash/BigBenBox ) for
273+ generating code in the C programming language.
274+
275+ ### Future
180276
181277Suggestions are welcome.
182278
183- Highlighting
184- ------------
279+ Highlighting in your editor
280+ ---------------------------
281+
282+ ### Vim
185283
186- To make PerlPP insets highlighted in Vim, add this to * ~ /.vimrc*
284+ To make highlight PerlPP insets in Vim, add this to * ~ /.vimrc*
187285
188286 autocmd colorscheme * hi PerlPP ctermbg=darkgrey ctermfg=lightgreen
189287
@@ -193,3 +291,9 @@ and create corresponding *~/.vim/after/syntax/FILETYPE.vim*
193291
194292FILETYPE can be determined with ` :set ft? `
195293
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+
0 commit comments