File: README.mkdn

package info (click to toggle)
libfilter-template-perl 1.043-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 168 kB
  • sloc: perl: 870; makefile: 2
file content (234 lines) | stat: -rw-r--r-- 7,125 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# NAME

Filter::Template - a source filter for inline code templates (macros)

# VERSION

version 1.043

# SYNOPSIS

	use Filter::Template;

	# use Filter::Template ( isa => 'SomeModule' );

	template max (one,two) {
		((one) > (two) ? (one) : (two))
	}

	print {% max $one, $two %}, "\n";

	const PI 3.14159265359

	print "PI\n";         # Constants are expanded inside strings.
	print "HAPPINESS\n";  # Also expanded due to naive parser.

	enum ZERO ONE TWO
	enum 12 TWELVE THIRTEEN FOURTEEN
	enum + FIFTEEN SIXTEEN SEVENTEEN

	# Prints numbers, due to naive parser.
	print "ZERO ONE TWO TWELVE THIRTEEN FOURTEEN FIFTEEN SIXTEEN SEVENTEEN\n";

	if ($expression) {      # include
		 ... lines of code ...
	}                       # include

	unless ($expression) {  # include
		... lines of code ...
	} elsif ($expression) { # include
		... lines of code ...
	} else {                # include
		... lines of code ...
	}                       # include

# DESCRIPTION

Filter::Template is a Perl source filter that provides simple inline
source code templates.  Inlined source code can be significantly
faster than subroutines, especially for small-scale functions like
accessors and mutators.  On the other hand, they are more difficult to
maintain and use.  Choose your trade-offs wisely.

## Templates

Code templates are defined with the `template` statement, which looks
a lot like `sub`.  Because this is a naive source filter, however,
the open brace must be on the same line as the `template` keyword.
Furthermore, the first closing brace in column zero ends a macro body.

	template oops {
		die "Oops";
	}

Templates are inserted into a program using a simple syntax that was
adapted from other template libraries.  It was chosen to be compatible
with the Perl syntax highlighting of common text editors.

This inserts the body of `template oops`.

	{% oops %}

Templates can have parameters.  The syntax for template parameters was
based on prototypes for Perl subroutines.  The two main differences
are that parameters are named, and sigils are not used.

	template sum_2 (parameter_0, parameter_1) {
		print( parameter_0 + parameter_1, "\n" );
	}

To insert a template with parameters, simply list the parameters after
the template name.

	{% sum_2 $base, $increment %}

At expansion time, occurrences of the parameter names within the
template are replaced with the source code provided in the template
invocation.  In the previous example, `sum_2` literally expands to

    print( $base + $increment, "\n" );

and is then compiled by Perl.

## Constants and Enumerations

Filter::Template also defines `const` and `enum` keywords.  They are
essentially simplified templates without parameters.

`const` defines a constant that is replaced before compile time.
Unlike Perl's native constants, these are not demoted to function
calls when Perl is run in debugging or profiling mode.

	const CONSTANT_NAME     'constant value'
	const ANOTHER_CONSTANT  23

Enumerations are like constants but several sequential integers can be
defined in one statement.  Enumerations start from zero by default:

	enum ZEROTH FIRST SECOND

If the first parameter of an enumeration is a number, then the
enumerated constants will start with that value:

	enum 10 TENTH ELEVENTH TWELFTH

Enumerations may not span lines, but they can be continued.  If the
first enumeration parameter is the plus sign, then constants will
start where the previous enumeration left off.

	enum 13 THIRTEENTH FOURTEENTH  FIFTEENTH
	enum +  SIXTEENTH  SEVENTEENTH EIGHTEENTH

## Conditional Code Inclusion (\#ifdef)

The preprocessor supports something like cpp's \#if/\#else/\#endif by
usurping a bit of Perl's conditional syntax.  The following
conditional statements will be evaluated at compile time if they are
followed by the comment `# include`:

	if (EXPRESSION) {      # include
		BLOCK;
	} elsif (EXPRESSION) { # include
		BLOCK;
	} else {               # include
		BLOCK;
	}                      # include

	unless (EXPRESSION) {  # include
		BLOCK;
	}                      # include

The code in each conditional statement's BLOCK will be included or
excluded in the compiled code depending on the outcome of its
EXPRESSION.

Conditional includes are nestable, but else and elsif must be on the
same line as the previous block's closing brace, as they are in the
previous example.

Filter::Template::UseBytes uses conditional code to define different
versions of a {% use\_bytes %} macro depending whether the `bytes`
pragma exists.

# IMPORTING TEMPLATES

Filter::Template can import templates defined by another class.  For
example, this invocation imports the `use_bytes` template:

	use Filter::Template ( isa => 'Filter::Template::UseBytes' );

Imported templates can be redefined in the current namespace.

Note: If the imported templates require additional Perl modules, any
code which imports them must also `use` those modules.

# DEBUGGING

Filter::Template has three debugging constants which will only take
effect if they are defined before the module is first used.

To trace source filtering in general, and to see the resulting code
and operations performed on each line, define:

	sub Filter::Template::DEBUG () { 1 }

To trace template invocations as they happen, define:

	sub Filter::Template::DEBUG_INVOKE () { 1 }

To see template, constant, and enum definitions, define:

	sub Filter::Template::DEBUG_DEFINE () { 1 }

To see warnings when a template or constant is redefined, define:

	sub Filter::Template::DEFINE () { 1 }

# CAVEATS

Source filters are line-based, and so is the template language.  The
only constructs that may span lines are template definitions, and
those __must__ span lines.

Filter::Template does not parse perl.  The regular expressions that
detect and replace code are simplistic and may not do the right things
when parsing challenging Perl syntax.  Constants are replaced within
strings, for example.

The regexp optimizer makes silly subexpressions like /(?:|m)/.  That
could be done better as /m?/ or /(?:jklm)?/ if the literal is longer
than a single character.

The regexp optimizer does not optimize (?:x|y|z) as character classes.

The regexp optimizer is based on code in Ilya Zakharevich's
Text::Trie.  Better regexp optimizers were released afterwards, and
Filter::Template should use one of them.

# LINKS

## BUG TRACKER

https://rt.cpan.org/Dist/Display.html?Status=Active&Queue=Filter-Template

## REPOSITORY

http://github.com/rcaputo/filter-template
http://gitorious.org/filter-template

## OTHER RESOURCES

http://search.cpan.org/dist/Filter-Template/

# SEE ALSO

[Text::Trie](http://search.cpan.org/perldoc?Text::Trie), [PAR](http://search.cpan.org/perldoc?PAR), [Filter::Template::UseBytes](http://search.cpan.org/perldoc?Filter::Template::UseBytes).

# AUTHOR & COPYRIGHT

Filter::Template is Copyright 2000-2013 Rocco Caputo.  Some parts are
Copyright 2001 Matt Cashner.  All rights reserved.  Filter::Template
is free software; you may redistribute it and/or modify it under the
same terms as Perl itself.

Filter::Template was previously known as POE::Preprocessor.