@@ -4,23 +4,45 @@ PerlPP: Perl preprocessor
4
4
Translates ** Text+Perl** to ** Text** .
5
5
It can be used for any kind of text templating, e.g. code generation.
6
6
No external modules are required, just a single file.
7
+ Requires Perl 5.10+.
7
8
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.
17
12
18
- Syntax
19
- ------
13
+ Usage
14
+ -----
20
15
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 ` <? ` :
24
46
25
47
<? code mode: Perl code is between the tags.
26
48
<?= echo mode: prints a Perl expression
@@ -41,6 +63,19 @@ produces the result:
41
63
42
64
<?x this tag is passed as is ?> because "x" is not a valid mode
43
65
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
+
44
79
Examples
45
80
--------
46
81
@@ -108,27 +143,43 @@ So `<?/ ... ?>` is effectively a shorthand for `<? print "\n"; ... ?>`.
108
143
Commands
109
144
--------
110
145
146
+ ### Include
147
+
111
148
<?:include file.p ?>
149
+ <?:include "long file name.p" ?>
112
150
113
- or ` <?:include "long file name.p" ?> ` (keep a whitespace between ` " ` and ` ?> ` , explained further).
114
151
Includes source code of another PerlPP file into this position.
115
152
Note that this file can be any PerlPP input, so you can also use this to
116
153
include 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
117
158
118
159
<?:prefix foo bar ?>
119
160
120
161
Replaces word prefixes in the following output.
121
162
In this case words like ` fooSomeWord ` will become ` barSomeWord ` .
122
163
164
+ ### Macro
165
+
123
166
<?:macro some_perl_code; ?>
124
167
125
168
will run ` some_perl_code; ` at the time of script generation. Whatever output
126
169
the perl code produces will be included verbatim in the script output.
127
170
This can be used to dynamically select which files you want to include,
128
- using
171
+ using the provided ` Include() ` function. For example:
129
172
130
173
<?:macro my $fn="some_name"; Include $fn; ?>
131
174
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
+
132
183
Capturing
133
184
---------
134
185
@@ -137,16 +188,16 @@ Sometimes it is great to get (capture) source text into a Perl string.
137
188
"?> start of capturing
138
189
<?" end of capturing
139
190
140
- For example
191
+ There must be no whitespace between the ` " ` and the ` ?> ` or ` <? ` . For example:
141
192
142
193
<? print "?>That's cool<?" . "?>, really.<?"; ?>
143
194
144
195
is the same as
145
196
146
197
<? print 'That\'s cool' . ', really.'; ?>
147
198
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!
150
201
151
202
<?
152
203
sub ABC {
@@ -160,30 +211,77 @@ Moreover, they can be nested!
160
211
<? ABC(); ?>
161
212
<?" ); ?>
162
213
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
165
216
166
217
abcdefghijklmnopqrstuvwxyz
167
218
ALPHABET
168
219
ABCDEFGHIJKLMNOPQRSTUVWXYZ
169
220
170
221
Capturing works in all modes: code, echo, or command mode.
171
222
172
- Custom Preprocessors
173
- --------------------
223
+ C Preprocessor Emulation
224
+ ------------------------
174
225
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.
177
230
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
180
276
181
277
Suggestions are welcome.
182
278
183
- Highlighting
184
- ------------
279
+ Highlighting in your editor
280
+ ---------------------------
281
+
282
+ ### Vim
185
283
186
- To make PerlPP insets highlighted in Vim, add this to * ~ /.vimrc*
284
+ To make highlight PerlPP insets in Vim, add this to * ~ /.vimrc*
187
285
188
286
autocmd colorscheme * hi PerlPP ctermbg=darkgrey ctermfg=lightgreen
189
287
@@ -193,3 +291,9 @@ and create corresponding *~/.vim/after/syntax/FILETYPE.vim*
193
291
194
292
FILETYPE can be determined with ` :set ft? `
195
293
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