File: perlfilter.html

package info (click to toggle)
perl-doc-html 5.26.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 39,400 kB
  • sloc: xml: 36; makefile: 7
file content (832 lines) | stat: -rw-r--r-- 57,754 bytes parent folder | download
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <title>perlfilter - perldoc.perl.org</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta http-equiv="Content-Language" content="en-gb">
  <link rel="search" type="application/opensearchdescription+xml" title="Search perldoc.perl.org" href="/static/search.xml"/>
  <link href="static/css-20100830.css" rel="stylesheet" rev="stylesheet" type="text/css" media="screen">
  <link href="static/exploreperl.css" rel="stylesheet" rev="stylesheet" type="text/css">
</head>

<body onLoad="perldoc.startup();" onPageShow="if (event.persisted) perldoc.startup();">
    <div id="page">
      
      <div id="header">
	<div id="homepage_link">
	  <a href="index.html"></a>
	</div>
	<div id="strapline">
	  Perl Programming Documentation
	</div>
	<div id="download_link" class="download">
	  <a href="http://www.perl.org/get.html">Download Perl</a>
	</div>
	<div id="explore_link" class="download">
	  <a id="explore_anchor" href="#">Explore</a>
	</div>
      </div>
      
      <div id="body">
        <div id="left_column">
          <div class="side_group">
            
	    <div class="side_panel doc_panel">
              <p>Manual</p>
              <ul>
                <li><a href="index-overview.html">Overview</a>
                <li><a href="index-tutorials.html">Tutorials</a>
                <li><a href="index-faq.html">FAQs</a>
                <li><a href="index-history.html">History / Changes</a>
                <li><a href="index-licence.html">License</a>
              </ul>
            </div>
            <div class="side_panel doc_panel">
              <p>Reference</p>
              <ul>
                <li><a href="index-language.html">Language</a>
                <li><a href="index-functions.html">Functions</a>
                <li><a href="perlop.html">Operators</a>
                <li><a href="perlvar.html">Special Variables</a>
                <li><a href="index-pragmas.html">Pragmas</a>
                <li><a href="index-utilities.html">Utilities</a>
                <li><a href="index-internals.html">Internals</a>
                <li><a href="index-platforms.html">Platform Specific</a>
              </ul>
            </div>
            <div class="side_panel doc_panel">
              <p>Modules</p>
              <ul>
		<li>
		
                
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		
                  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		
                  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		
                  
		
                  
		    
		  
		
                  
		
                  
		
		
                    <a href="index-modules-A.html">A</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-B.html">B</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-C.html">C</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-D.html">D</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-E.html">E</a>
                    
                      
                        <li>
                      
                    
                
                    <a href="index-modules-F.html">F</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-G.html">G</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-H.html">H</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-I.html">I</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-L.html">L</a>
                    
                      
                        <li>
                      
                    
                
                    <a href="index-modules-M.html">M</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-N.html">N</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-O.html">O</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-P.html">P</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-S.html">S</a>
                    
                      
                        <li>
                      
                    
                
                    <a href="index-modules-T.html">T</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-U.html">U</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-X.html">X</a>
                    
                
              </ul>
            </div>
            
	      <div class="side_panel doc_panel">
		<p>Tools</p>
		<ul>
		  <li><a href="preferences.html">Preferences</a>
		</ul>
	      </div>
            
          </div>
        </div>
        <div id="centre_column">
          <div id="content_header">
            <div id="title_bar">
              <div id="page_name">
                <h1>perlfilter</h1>
              </div>
              <div id="perl_version">
                Perl 5 version 26.0 documentation
              </div>
              <div class="page_links" id="page_links_top">
                <a href="#" onClick="toolbar.goToTop();return false;">Go to top</a>
		
              </div>
	      <div class="page_links" id="page_links_bottom">
		
                  <a href="#" id="page_index_toggle">Show page index</a> &bull;
		
                <a href="#" id="recent_pages_toggle">Show recent pages</a>		
	      </div>
	      <div id="search_form">
		<form action="search.html" method="GET" id="search">
		  <input type="text" name="q" id="search_box" alt="Search">
		</form>
	      </div>
            </div>
            <div id="breadcrumbs">
                
    <a href="index.html">Home</a> &gt;
    
      
        <a href="index-language.html">Language reference</a> &gt;
      
    
    perlfilter
  

            </div>
          </div>
          <div id="content_body">
	    <!--[if lt IE 7]>
 <div class="noscript">
   <p>
     <strong>It looks like you're using Internet Explorer 6. This is a very old
     browser which does not offer full support for modern websites.</strong>
   </p>
   <p>
     Unfortunately this means that this website will not work on
     your computer.
   </p>
   <p>
     Don't miss out though! To view the site (and get a better experience from
     many other websites), simply upgrade to
     <a href="http://www.microsoft.com/windows/Internet-explorer/default.aspx">Internet
Explorer 8</a>
     or download an alternative browser such as
     <a href="http://www.mozilla.com/en-US/firefox/firefox.html">Firefox</a>,
     <a href="http://www.apple.com/safari/download/">Safari</a>, or
     <a href="http://www.google.co.uk/chrome">Google Chrome</a>.
   </p>
   <p>
     All of these browsers are free. If you're using a PC at work, you may
     need to contact your IT administrator.
   </p>
 </div>
<![endif]-->
	    <noscript>
	      <div class="noscript">
	      <p>
                <strong>Please note: Many features of this site require JavaScript. You appear to have JavaScript disabled,
	        or are running a non-JavaScript capable web browser.</strong>
	      </p>
	      <p>
		To get the best experience, please enable JavaScript or download a modern web browser such as <a href="http://www.microsoft.com/windows/Internet-explorer/default.aspx">Internet Explorer 8</a>, <a href="http://www.mozilla.com/en-US/firefox/firefox.html">Firefox</a>, <a href="http://www.apple.com/safari/download/">Safari</a>, or <a href="http://www.google.co.uk/chrome">Google Chrome</a>.
              </p>
	      </div>
	    </noscript>

	    <div id="recent_pages" class="hud_container">
	      <div id="recent_pages_header" class="hud_header">
		<div id="recent_pages_close" class="hud_close"><a href="#" onClick="recentPages.hide();return false;"></a></div>
		<div id="recent_pages_title" class="hud_title"><span class="hud_span_top">Recently read</span></div>
		<div id="recent_pages_topright" class="hud_topright"></div>
	      </div>
	      <div id="recent_pages_content" class="hud_content">
	      </div>
	      <div id="recent_pages_footer" class="hud_footer">
		<div id="recent_pages_bottomleft" class="hud_bottomleft"></div>
		<div id="recent_pages_bottom" class="hud_bottom"><span class="hud_span_bottom"></span></div>
		<div id="recent_pages_resize" class="hud_resize"></div>
	      </div>
	    </div>
  
	    <div id="from_search"></div>
            <h1>perlfilter</h1>


  <!--    -->
<ul><li><a href="#NAME">NAME</a><li><a href="#DESCRIPTION">DESCRIPTION</a><li><a href="#CONCEPTS">CONCEPTS</a><li><a href="#USING-FILTERS">USING FILTERS</a><li><a href="#WRITING-A-SOURCE-FILTER">WRITING A SOURCE FILTER</a><li><a href="#WRITING-A-SOURCE-FILTER-IN-C">WRITING A SOURCE FILTER IN C</a><li><a href="#CREATING-A-SOURCE-FILTER-AS-A-SEPARATE-EXECUTABLE">CREATING A SOURCE FILTER AS A SEPARATE EXECUTABLE</a><li><a href="#WRITING-A-SOURCE-FILTER-IN-PERL">WRITING A SOURCE FILTER IN PERL</a><li><a href="#USING-CONTEXT%3a-THE-DEBUG-FILTER">USING CONTEXT: THE DEBUG FILTER</a><li><a href="#CONCLUSION">CONCLUSION</a><li><a href="#LIMITATIONS">LIMITATIONS</a><li><a href="#THINGS-TO-LOOK-OUT-FOR">THINGS TO LOOK OUT FOR</a><li><a href="#REQUIREMENTS">REQUIREMENTS</a><li><a href="#AUTHOR">AUTHOR</a><li><a href="#Copyrights">Copyrights</a></ul><a name="NAME"></a><h1>NAME</h1>
<p>perlfilter - Source Filters</p>
<a name="DESCRIPTION"></a><h1>DESCRIPTION</h1>
<p>This article is about a little-known feature of Perl called
<i>source filters</i>. Source filters alter the program text of a module
before Perl sees it, much as a C preprocessor alters the source text of
a C program before the compiler sees it. This article tells you more
about what source filters are, how they work, and how to write your
own.</p>
<p>The original purpose of source filters was to let you encrypt your
program source to prevent casual piracy. This isn't all they can do, as
you'll soon learn. But first, the basics.</p>
<a name="CONCEPTS"></a><h1>CONCEPTS</h1>
<p>Before the Perl interpreter can execute a Perl script, it must first
read it from a file into memory for parsing and compilation. If that
script itself includes other scripts with a <code class="inline"><a class="l_k" href="functions/use.html">use</a></code> or <code class="inline"><a class="l_k" href="functions/require.html">require</a></code>
statement, then each of those scripts will have to be read from their
respective files as well.</p>
<p>Now think of each logical connection between the Perl parser and an
individual file as a <i>source stream</i>. A source stream is created when
the Perl parser opens a file, it continues to exist as the source code
is read into memory, and it is destroyed when Perl is finished parsing
the file. If the parser encounters a <code class="inline"><a class="l_k" href="functions/require.html">require</a></code> or <code class="inline"><a class="l_k" href="functions/use.html">use</a></code> statement in
a source stream, a new and distinct stream is created just for that
file.</p>
<p>The diagram below represents a single source stream, with the flow of
source from a Perl script file on the left into the Perl parser on the
right. This is how Perl normally operates.</p>
<pre class="verbatim"><ol><li>    <span class="w">file</span> ------<span class="i">-&gt; parser</span></li></ol></pre><p>There are two important points to remember:</p>
<dl>
<dt>1.</dt><dd>
<p>Although there can be any number of source streams in existence at any
given time, only one will be active.</p>
</dd>
<dt>2.</dt><dd>
<p>Every source stream is associated with only one file.</p>
</dd>
</dl>
<p>A source filter is a special kind of Perl module that intercepts and
modifies a source stream before it reaches the parser. A source filter
changes our diagram like this:</p>
<pre class="verbatim"><ol><li>    <span class="w">file</span> ----&gt; <span class="w">filter</span> ----&gt; <span class="w">parser</span></li></ol></pre><p>If that doesn't make much sense, consider the analogy of a command
pipeline. Say you have a shell script stored in the compressed file
<i>trial.gz</i>. The simple pipeline command below runs the script without
needing to create a temporary file to hold the uncompressed file.</p>
<pre class="verbatim"><ol><li>    <span class="w">gunzip</span> -c <span class="w">trial</span>.<span class="w">gz</span> | <span class="w">sh</span></li></ol></pre><p>In this case, the data flow from the pipeline can be represented as follows:</p>
<pre class="verbatim"><ol><li>    <span class="w">trial</span>.<span class="w">gz</span> ----&gt; <span class="w">gunzip</span> ----&gt; <span class="w">sh</span></li></ol></pre><p>With source filters, you can store the text of your script compressed and use a source filter to uncompress it for Perl's parser:</p>
<pre class="verbatim"><ol><li>     compressed           gunzip</li><li>    Perl program ---&gt; source filter ---&gt; parser</li></ol></pre><a name="USING-FILTERS"></a><h1>USING FILTERS</h1>
<p>So how do you use a source filter in a Perl script? Above, I said that
a source filter is just a special kind of module. Like all Perl
modules, a source filter is invoked with a use statement.</p>
<p>Say you want to pass your Perl source through the C preprocessor before
execution. As it happens, the source filters distribution comes with a C
preprocessor filter module called Filter::cpp.</p>
<p>Below is an example program, <code class="inline"><span class="w">cpp_test</span></code>
, which makes use of this filter.
Line numbers have been added to allow specific lines to be referenced
easily.</p>
<pre class="verbatim"><ol><li>    1: use Filter::cpp;</li><li>    2: #define TRUE 1</li><li>    3: $a = TRUE;</li><li>    4: print "a = $a\n";</li></ol></pre><p>When you execute this script, Perl creates a source stream for the
file. Before the parser processes any of the lines from the file, the
source stream looks like this:</p>
<pre class="verbatim"><ol><li>    <span class="w">cpp_test</span> --------<span class="i">-&gt; parser</span></li></ol></pre><p>Line 1, <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">Filter::cpp</span></code>
, includes and installs the <code class="inline"><span class="w">cpp</span></code>
 filter
module. All source filters work this way. The use statement is compiled
and executed at compile time, before any more of the file is read, and
it attaches the cpp filter to the source stream behind the scenes. Now
the data flow looks like this:</p>
<pre class="verbatim"><ol><li>    <span class="w">cpp_test</span> ----&gt; <span class="w">cpp</span> <span class="w">filter</span> ----&gt; <span class="w">parser</span></li></ol></pre><p>As the parser reads the second and subsequent lines from the source
stream, it feeds those lines through the <code class="inline"><span class="w">cpp</span></code>
 source filter before
processing them. The <code class="inline"><span class="w">cpp</span></code>
 filter simply passes each line through the
real C preprocessor. The output from the C preprocessor is then
inserted back into the source stream by the filter.</p>
<pre class="verbatim"><ol><li>                  .-&gt; cpp --.</li><li>                  |         |</li><li>                  |         |</li><li>                  |       &lt;-'</li><li>   cpp_test ----&gt; cpp filter ----&gt; parser</li></ol></pre><p>The parser then sees the following code:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">Filter::cpp</span><span class="sc">;</span></li><li>    <span class="i">$a</span> = <span class="n">1</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;a = $a\n&quot;</span><span class="sc">;</span></li></ol></pre><p>Let's consider what happens when the filtered code includes another
module with use:</p>
<pre class="verbatim"><ol><li>    1: use Filter::cpp;</li><li>    2: #define TRUE 1</li><li>    3: use Fred;</li><li>    4: $a = TRUE;</li><li>    5: print "a = $a\n";</li></ol></pre><p>The <code class="inline"><span class="w">cpp</span></code>
 filter does not apply to the text of the Fred module, only
to the text of the file that used it (<code class="inline"><span class="w">cpp_test</span></code>
). Although the use
statement on line 3 will pass through the cpp filter, the module that
gets included (<code class="inline"><span class="w">Fred</span></code>
) will not. The source streams look like this
after line 3 has been parsed and before line 4 is parsed:</p>
<pre class="verbatim"><ol><li>    cpp_test ---&gt; cpp filter ---&gt; parser (INACTIVE)</li><li></li><li>    Fred.pm ----&gt; parser</li></ol></pre><p>As you can see, a new stream has been created for reading the source
from <code class="inline"><span class="w">Fred</span>.<span class="w">pm</span></code>
. This stream will remain active until all of <code class="inline"><span class="w">Fred</span>.<span class="w">pm</span></code>

has been parsed. The source stream for <code class="inline"><span class="w">cpp_test</span></code>
 will still exist,
but is inactive. Once the parser has finished reading Fred.pm, the
source stream associated with it will be destroyed. The source stream
for <code class="inline"><span class="w">cpp_test</span></code>
 then becomes active again and the parser reads line 4
and subsequent lines from <code class="inline"><span class="w">cpp_test</span></code>
.</p>
<p>You can use more than one source filter on a single file. Similarly,
you can reuse the same filter in as many files as you like.</p>
<p>For example, if you have a uuencoded and compressed source file, it is
possible to stack a uudecode filter and an uncompression filter like
this:</p>
<pre class="verbatim"><ol><li>    use Filter::uudecode; use Filter::uncompress;</li><li>    M'XL(".H&lt;US4''V9I;F%L')Q;&gt;7/;1I;_&gt;_I3=&amp;E=%:F*I"T?22Q/</li><li>    M6]9*&lt;IQCO*XFT"0[PL%%'Y+IG?WN^ZYN-$'J.[.JE$,20/?K=_[&gt;</li><li>    ...</li></ol></pre><p>Once the first line has been processed, the flow will look like this:</p>
<pre class="verbatim"><ol><li>    file ---&gt; uudecode ---&gt; uncompress ---&gt; parser</li><li>               filter         filter</li></ol></pre><p>Data flows through filters in the same order they appear in the source
file. The uudecode filter appeared before the uncompress filter, so the
source file will be uudecoded before it's uncompressed.</p>
<a name="WRITING-A-SOURCE-FILTER"></a><h1>WRITING A SOURCE FILTER</h1>
<p>There are three ways to write your own source filter. You can write it
in C, use an external program as a filter, or write the filter in Perl.
I won't cover the first two in any great detail, so I'll get them out
of the way first. Writing the filter in Perl is most convenient, so
I'll devote the most space to it.</p>
<a name="WRITING-A-SOURCE-FILTER-IN-C"></a><h1>WRITING A SOURCE FILTER IN C</h1>
<p>The first of the three available techniques is to write the filter
completely in C. The external module you create interfaces directly
with the source filter hooks provided by Perl.</p>
<p>The advantage of this technique is that you have complete control over
the implementation of your filter. The big disadvantage is the
increased complexity required to write the filter - not only do you
need to understand the source filter hooks, but you also need a
reasonable knowledge of Perl guts. One of the few times it is worth
going to this trouble is when writing a source scrambler. The
<code class="inline"><span class="w">decrypt</span></code>
 filter (which unscrambles the source before Perl parses it)
included with the source filter distribution is an example of a C
source filter (see Decryption Filters, below).</p>
<ul>
<li><a name="*Decryption-Filters*"></a><b><b>Decryption Filters</b></b>
<p>All decryption filters work on the principle of "security through
obscurity." Regardless of how well you write a decryption filter and
how strong your encryption algorithm is, anyone determined enough can
retrieve the original source code. The reason is quite simple - once
the decryption filter has decrypted the source back to its original
form, fragments of it will be stored in the computer's memory as Perl
parses it. The source might only be in memory for a short period of
time, but anyone possessing a debugger, skill, and lots of patience can
eventually reconstruct your program.</p>
<p>That said, there are a number of steps that can be taken to make life
difficult for the potential cracker. The most important: Write your
decryption filter in C and statically link the decryption module into
the Perl binary. For further tips to make life difficult for the
potential cracker, see the file <i>decrypt.pm</i> in the source filters
distribution.</p>
</li>
</ul>
<a name="CREATING-A-SOURCE-FILTER-AS-A-SEPARATE-EXECUTABLE"></a><h1>CREATING A SOURCE FILTER AS A SEPARATE EXECUTABLE</h1>
<p>An alternative to writing the filter in C is to create a separate
executable in the language of your choice. The separate executable
reads from standard input, does whatever processing is necessary, and
writes the filtered data to standard output. <code class="inline"><span class="w">Filter::cpp</span></code>
 is an
example of a source filter implemented as a separate executable - the
executable is the C preprocessor bundled with your C compiler.</p>
<p>The source filter distribution includes two modules that simplify this
task: <code class="inline"><span class="w">Filter::exec</span></code>
 and <code class="inline"><span class="w">Filter::sh</span></code>
. Both allow you to run any
external executable. Both use a coprocess to control the flow of data
into and out of the external executable. (For details on coprocesses,
see Stephens, W.R., "Advanced Programming in the UNIX Environment."
Addison-Wesley, ISBN 0-210-56317-7, pages 441-445.) The difference
between them is that <code class="inline"><span class="w">Filter::exec</span></code>
 spawns the external command
directly, while <code class="inline"><span class="w">Filter::sh</span></code>
 spawns a shell to execute the external
command. (Unix uses the Bourne shell; NT uses the cmd shell.) Spawning
a shell allows you to make use of the shell metacharacters and
redirection facilities.</p>
<p>Here is an example script that uses <code class="inline"><span class="w">Filter::sh</span></code>
:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">Filter::sh</span> <span class="q">&#39;tr XYZ PQR&#39;</span><span class="sc">;</span></li><li>    <span class="i">$a</span> = <span class="n">1</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;XYZ a = $a\n&quot;</span><span class="sc">;</span></li></ol></pre><p>The output you'll get when the script is executed:</p>
<pre class="verbatim"><ol><li>    <span class="w">PQR</span> <span class="w">a</span> = <span class="n">1</span></li></ol></pre><p>Writing a source filter as a separate executable works fine, but a
small performance penalty is incurred. For example, if you execute the
small example above, a separate subprocess will be created to run the
Unix <code class="inline"><a class="l_k" href="functions/tr.html">tr</a></code> command. Each use of the filter requires its own subprocess.
If creating subprocesses is expensive on your system, you might want to
consider one of the other options for creating source filters.</p>
<a name="WRITING-A-SOURCE-FILTER-IN-PERL"></a><h1>WRITING A SOURCE FILTER IN PERL</h1>
<p>The easiest and most portable option available for creating your own
source filter is to write it completely in Perl. To distinguish this
from the previous two techniques, I'll call it a Perl source filter.</p>
<p>To help understand how to write a Perl source filter we need an example
to study. Here is a complete source filter that performs rot13
decoding. (Rot13 is a very simple encryption scheme used in Usenet
postings to hide the contents of offensive posts. It moves every letter
forward thirteen places, so that A becomes N, B becomes O, and Z
becomes M.)</p>
<pre class="verbatim"><ol><li><a name="package-Rot13"></a>   package <span class="i">Rot13</span><span class="sc">;</span></li><li></li><li>   <a class="l_k" href="functions/use.html">use</a> <span class="w">Filter::Util::Call</span><span class="sc">;</span></li><li></li><li><a name="import"></a>   sub <span class="m">import</span> <span class="s">{</span></li><li>      <a class="l_k" href="functions/my.html">my</a> <span class="s">(</span><span class="i">$type</span><span class="s">)</span> = <span class="i">@_</span><span class="sc">;</span></li><li>      <a class="l_k" href="functions/my.html">my</a> <span class="s">(</span><span class="i">$ref</span><span class="s">)</span> = <span class="s">[</span><span class="s">]</span><span class="sc">;</span></li><li>      <span class="i">filter_add</span><span class="s">(</span><a class="l_k" href="functions/bless.html">bless</a> <span class="i">$ref</span><span class="s">)</span><span class="sc">;</span></li><li>   <span class="s">}</span></li><li></li><li><a name="filter"></a>   sub <span class="m">filter</span> <span class="s">{</span></li><li>      <a class="l_k" href="functions/my.html">my</a> <span class="s">(</span><span class="i">$self</span><span class="s">)</span> = <span class="i">@_</span><span class="sc">;</span></li><li>      <a class="l_k" href="functions/my.html">my</a> <span class="s">(</span><span class="i">$status</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>      <span class="q">tr/n-za-mN-ZA-M/a-zA-Z/</span></li><li>         if <span class="s">(</span><span class="i">$status</span> = <span class="i">filter_read</span><span class="s">(</span><span class="s">)</span><span class="s">)</span> &gt; <span class="n">0</span><span class="sc">;</span></li><li>      <span class="i">$status</span><span class="sc">;</span></li><li>   <span class="s">}</span></li><li></li><li>   <span class="n">1</span><span class="sc">;</span></li></ol></pre><p>All Perl source filters are implemented as Perl classes and have the
same basic structure as the example above.</p>
<p>First, we include the <code class="inline"><span class="w">Filter::Util::Call</span></code>
 module, which exports a
number of functions into your filter's namespace. The filter shown
above uses two of these functions, <code class="inline"><span class="i">filter_add</span><span class="s">(</span><span class="s">)</span></code>
 and
<code class="inline"><span class="i">filter_read</span><span class="s">(</span><span class="s">)</span></code>
.</p>
<p>Next, we create the filter object and associate it with the source
stream by defining the <code class="inline"><a class="l_k" href="functions/import.html">import</a></code> function. If you know Perl well
enough, you know that <code class="inline"><a class="l_k" href="functions/import.html">import</a></code> is called automatically every time a
module is included with a use statement. This makes <code class="inline"><a class="l_k" href="functions/import.html">import</a></code> the ideal
place to both create and install a filter object.</p>
<p>In the example filter, the object (<code class="inline"><span class="i">$ref</span></code>
) is blessed just like any
other Perl object. Our example uses an anonymous array, but this isn't
a requirement. Because this example doesn't need to store any context
information, we could have used a scalar or hash reference just as
well. The next section demonstrates context data.</p>
<p>The association between the filter object and the source stream is made
with the <code class="inline"><span class="i">filter_add</span><span class="s">(</span><span class="s">)</span></code>
 function. This takes a filter object as a
parameter (<code class="inline"><span class="i">$ref</span></code>
 in this case) and installs it in the source stream.</p>
<p>Finally, there is the code that actually does the filtering. For this
type of Perl source filter, all the filtering is done in a method
called <code class="inline"><span class="i">filter</span><span class="s">(</span><span class="s">)</span></code>
. (It is also possible to write a Perl source filter
using a closure. See the <code class="inline"><span class="w">Filter::Util::Call</span></code>
 manual page for more
details.) It's called every time the Perl parser needs another line of
source to process. The <code class="inline"><span class="i">filter</span><span class="s">(</span><span class="s">)</span></code>
 method, in turn, reads lines from
the source stream using the <code class="inline"><span class="i">filter_read</span><span class="s">(</span><span class="s">)</span></code>
 function.</p>
<p>If a line was available from the source stream, <code class="inline"><span class="i">filter_read</span><span class="s">(</span><span class="s">)</span></code>

returns a status value greater than zero and appends the line to <code class="inline"><span class="i">$_</span></code>
.
A status value of zero indicates end-of-file, less than zero means an
error. The filter function itself is expected to return its status in
the same way, and put the filtered line it wants written to the source
stream in <code class="inline"><span class="i">$_</span></code>
. The use of <code class="inline"><span class="i">$_</span></code>
 accounts for the brevity of most Perl
source filters.</p>
<p>In order to make use of the rot13 filter we need some way of encoding
the source file in rot13 format. The script below, <code class="inline"><span class="w">mkrot13</span></code>
, does
just that.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;usage mkrot13 filename\n&quot;</span> unless <span class="i">@ARGV</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$in</span> = <span class="i">$ARGV</span>[<span class="n">0</span>]<span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$out</span> = <span class="q">&quot;$in.tmp&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/open.html">open</a><span class="s">(</span><span class="w">IN</span><span class="cm">,</span> <span class="q">&quot;&lt;$in&quot;</span><span class="s">)</span> or <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;Cannot open file $in: $!\n&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/open.html">open</a><span class="s">(</span><span class="w">OUT</span><span class="cm">,</span> <span class="q">&quot;&gt;$out&quot;</span><span class="s">)</span> or <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;Cannot open file $out: $!\n&quot;</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/print.html">print</a> <span class="i">OUT</span> <span class="q">&quot;use Rot13;\n&quot;</span><span class="sc">;</span></li><li>    while <span class="s">(</span><span class="q">&lt;IN&gt;</span><span class="s">)</span> <span class="s">{</span></li><li>       <span class="q">tr/a-zA-Z/n-za-mN-ZA-M/</span><span class="sc">;</span></li><li>       <a class="l_k" href="functions/print.html">print</a> <span class="i">OUT</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li></li><li>    <a class="l_k" href="functions/close.html">close</a> <span class="w">IN</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/close.html">close</a> <span class="w">OUT</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/unlink.html">unlink</a> <span class="i">$in</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/rename.html">rename</a> <span class="i">$out</span><span class="cm">,</span> <span class="i">$in</span><span class="sc">;</span></li></ol></pre><p>If we encrypt this with <code class="inline"><span class="w">mkrot13</span></code>
:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot; hello fred \n&quot;</span><span class="sc">;</span></li></ol></pre><p>the result will be this:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">Rot13</span><span class="sc">;</span></li><li>    <span class="w">cevag</span> <span class="q">&quot;uryyb serq\a&quot;</span><span class="sc">;</span></li></ol></pre><p>Running it produces this output:</p>
<pre class="verbatim"><ol><li>    <span class="w">hello</span> <span class="w">fred</span></li></ol></pre><a name="USING-CONTEXT%3a-THE-DEBUG-FILTER"></a><h1>USING CONTEXT: THE DEBUG FILTER</h1>
<p>The rot13 example was a trivial example. Here's another demonstration
that shows off a few more features.</p>
<p>Say you wanted to include a lot of debugging code in your Perl script
during development, but you didn't want it available in the released
product. Source filters offer a solution. In order to keep the example
simple, let's say you wanted the debugging output to be controlled by
an environment variable, <code class="inline"><span class="w">DEBUG</span></code>
. Debugging code is enabled if the
variable exists, otherwise it is disabled.</p>
<p>Two special marker lines will bracket debugging code, like this:</p>
<pre class="verbatim"><ol><li>    <span class="c">## DEBUG_BEGIN</span></li><li>    if <span class="s">(</span><span class="i">$year</span> &gt; <span class="n">1999</span><span class="s">)</span> <span class="s">{</span></li><li>       <a class="l_k" href="functions/warn.html">warn</a> <span class="q">&quot;Debug: millennium bug in year $year\n&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li>    <span class="c">## DEBUG_END</span></li></ol></pre><p>The filter ensures that Perl parses the code between the &lt;DEBUG_BEGIN&gt;
and <code class="inline"><span class="w">DEBUG_END</span></code>
 markers only when the <code class="inline"><span class="w">DEBUG</span></code>
 environment variable
exists. That means that when <code class="inline"><span class="w">DEBUG</span></code>
 does exist, the code above
should be passed through the filter unchanged. The marker lines can
also be passed through as-is, because the Perl parser will see them as
comment lines. When <code class="inline"><span class="w">DEBUG</span></code>
 isn't set, we need a way to disable the
debug code. A simple way to achieve that is to convert the lines
between the two markers into comments:</p>
<pre class="verbatim"><ol><li>    <span class="c">## DEBUG_BEGIN</span></li><li>    <span class="c">#if ($year &gt; 1999) {</span></li><li>    <span class="c">#     warn &quot;Debug: millennium bug in year $year\n&quot;;</span></li><li>    <span class="c">#}</span></li><li>    <span class="c">## DEBUG_END</span></li></ol></pre><p>Here is the complete Debug filter:</p>
<pre class="verbatim"><ol><li><a name="package-Debug"></a>    package <span class="i">Debug</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">strict</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">Filter::Util::Call</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">constant</span> <span class="i">TRUE</span> <span class="cm">=&gt;</span> <span class="n">1</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">constant</span> <span class="i">FALSE</span> <span class="cm">=&gt;</span> <span class="n">0</span><span class="sc">;</span></li><li></li><li><a name="import"></a>    sub <span class="m">import</span> <span class="s">{</span></li><li>       <a class="l_k" href="functions/my.html">my</a> <span class="s">(</span><span class="i">$type</span><span class="s">)</span> = <span class="i">@_</span><span class="sc">;</span></li><li>       <a class="l_k" href="functions/my.html">my</a> <span class="s">(</span><span class="i">%context</span><span class="s">)</span> = <span class="s">(</span></li><li>         <span class="w">Enabled</span> <span class="cm">=&gt;</span> <a class="l_k" href="functions/defined.html">defined</a> <span class="i">$ENV</span>{<span class="w">DEBUG</span>}<span class="cm">,</span></li><li>         <span class="w">InTraceBlock</span> <span class="cm">=&gt;</span> <span class="i">FALSE</span><span class="cm">,</span></li><li>         <span class="w">Filename</span> <span class="cm">=&gt;</span> <span class="s">(</span><a class="l_k" href="functions/caller.html">caller</a><span class="s">)</span>[<span class="n">1</span>]<span class="cm">,</span></li><li>         <span class="w">LineNo</span> <span class="cm">=&gt;</span> <span class="n">0</span><span class="cm">,</span></li><li>         <span class="w">LastBegin</span> <span class="cm">=&gt;</span> <span class="n">0</span><span class="cm">,</span></li><li>       <span class="s">)</span><span class="sc">;</span></li><li>       <span class="i">filter_add</span><span class="s">(</span><a class="l_k" href="functions/bless.html">bless</a> \<span class="i">%context</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li></li><li><a name="Die"></a>    sub <span class="m">Die</span> <span class="s">{</span></li><li>       <a class="l_k" href="functions/my.html">my</a> <span class="s">(</span><span class="i">$self</span><span class="s">)</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>       <a class="l_k" href="functions/my.html">my</a> <span class="s">(</span><span class="i">$message</span><span class="s">)</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>       <a class="l_k" href="functions/my.html">my</a> <span class="s">(</span><span class="i">$line_no</span><span class="s">)</span> = <a class="l_k" href="functions/shift.html">shift</a> || <span class="i">$self</span>-&gt;{<span class="w">LastBegin</span>}<span class="sc">;</span></li><li>       <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;$message at $self-&gt;{Filename} line $line_no.\n&quot;</span></li><li>    <span class="s">}</span></li><li></li><li><a name="filter"></a>    sub <span class="m">filter</span> <span class="s">{</span></li><li>       <a class="l_k" href="functions/my.html">my</a> <span class="s">(</span><span class="i">$self</span><span class="s">)</span> = <span class="i">@_</span><span class="sc">;</span></li><li>       <a class="l_k" href="functions/my.html">my</a> <span class="s">(</span><span class="i">$status</span><span class="s">)</span><span class="sc">;</span></li><li>       <span class="i">$status</span> = <span class="i">filter_read</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>       ++ <span class="i">$self</span>-&gt;{<span class="w">LineNo</span>}<span class="sc">;</span></li><li></li><li>       <span class="c"># deal with EOF/error first</span></li><li>       if <span class="s">(</span><span class="i">$status</span> &lt;= <span class="n">0</span><span class="s">)</span> <span class="s">{</span></li><li>           <span class="i">$self</span><span class="i">-&gt;Die</span><span class="s">(</span><span class="q">&quot;DEBUG_BEGIN has no DEBUG_END&quot;</span><span class="s">)</span></li><li>               if <span class="i">$self</span>-&gt;{<span class="w">InTraceBlock</span>}<span class="sc">;</span></li><li>           <a class="l_k" href="functions/return.html">return</a> <span class="i">$status</span><span class="sc">;</span></li><li>       <span class="s">}</span></li><li></li><li>       if <span class="s">(</span><span class="i">$self</span>-&gt;{<span class="w">InTraceBlock</span>}<span class="s">)</span> <span class="s">{</span></li><li>          if <span class="s">(</span><span class="q">/^\s*##\s*DEBUG_BEGIN/</span> <span class="s">)</span> <span class="s">{</span></li><li>              <span class="i">$self</span><span class="i">-&gt;Die</span><span class="s">(</span><span class="q">&quot;Nested DEBUG_BEGIN&quot;</span><span class="cm">,</span> <span class="i">$self</span>-&gt;{<span class="w">LineNo</span>}<span class="s">)</span></li><li>          <span class="s">}</span> elsif <span class="s">(</span><span class="q">/^\s*##\s*DEBUG_END/</span><span class="s">)</span> <span class="s">{</span></li><li>              <span class="i">$self</span>-&gt;{<span class="w">InTraceBlock</span>} = <span class="i">FALSE</span><span class="sc">;</span></li><li>          <span class="s">}</span></li><li></li><li>          <span class="c"># comment out the debug lines when the filter is disabled</span></li><li>          <span class="q">s/^/#/</span> if ! <span class="i">$self</span>-&gt;{<span class="w">Enabled</span>}<span class="sc">;</span></li><li>       <span class="s">}</span> elsif <span class="s">(</span> <span class="q">/^\s*##\s*DEBUG_BEGIN/</span> <span class="s">)</span> <span class="s">{</span></li><li>          <span class="i">$self</span>-&gt;{<span class="w">InTraceBlock</span>} = <span class="i">TRUE</span><span class="sc">;</span></li><li>          <span class="i">$self</span>-&gt;{<span class="w">LastBegin</span>} = <span class="i">$self</span>-&gt;{<span class="w">LineNo</span>}<span class="sc">;</span></li><li>       <span class="s">}</span> elsif <span class="s">(</span> <span class="q">/^\s*##\s*DEBUG_END/</span> <span class="s">)</span> <span class="s">{</span></li><li>          <span class="i">$self</span><span class="i">-&gt;Die</span><span class="s">(</span><span class="q">&quot;DEBUG_END has no DEBUG_BEGIN&quot;</span><span class="cm">,</span> <span class="i">$self</span>-&gt;{<span class="w">LineNo</span>}<span class="s">)</span><span class="sc">;</span></li><li>       <span class="s">}</span></li><li>       <a class="l_k" href="functions/return.html">return</a> <span class="i">$status</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li></li><li>    <span class="n">1</span><span class="sc">;</span></li></ol></pre><p>The big difference between this filter and the previous example is the
use of context data in the filter object. The filter object is based on
a hash reference, and is used to keep various pieces of context
information between calls to the filter function. All but two of the
hash fields are used for error reporting. The first of those two,
Enabled, is used by the filter to determine whether the debugging code
should be given to the Perl parser. The second, InTraceBlock, is true
when the filter has encountered a <code class="inline"><span class="w">DEBUG_BEGIN</span></code>
 line, but has not yet
encountered the following <code class="inline"><span class="w">DEBUG_END</span></code>
 line.</p>
<p>If you ignore all the error checking that most of the code does, the
essence of the filter is as follows:</p>
<pre class="verbatim"><ol><li><a name="filter"></a>    sub <span class="m">filter</span> <span class="s">{</span></li><li>       <a class="l_k" href="functions/my.html">my</a> <span class="s">(</span><span class="i">$self</span><span class="s">)</span> = <span class="i">@_</span><span class="sc">;</span></li><li>       <a class="l_k" href="functions/my.html">my</a> <span class="s">(</span><span class="i">$status</span><span class="s">)</span><span class="sc">;</span></li><li>       <span class="i">$status</span> = <span class="i">filter_read</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>       <span class="c"># deal with EOF/error first</span></li><li>       <a class="l_k" href="functions/return.html">return</a> <span class="i">$status</span> if <span class="i">$status</span> &lt;= <span class="n">0</span><span class="sc">;</span></li><li>       if <span class="s">(</span><span class="i">$self</span>-&gt;{<span class="w">InTraceBlock</span>}<span class="s">)</span> <span class="s">{</span></li><li>          if <span class="s">(</span><span class="q">/^\s*##\s*DEBUG_END/</span><span class="s">)</span> <span class="s">{</span></li><li>             <span class="i">$self</span>-&gt;{<span class="w">InTraceBlock</span>} = <span class="w">FALSE</span></li><li>          <span class="s">}</span></li><li></li><li>          <span class="c"># comment out debug lines when the filter is disabled</span></li><li>          <span class="q">s/^/#/</span> if ! <span class="i">$self</span>-&gt;{<span class="w">Enabled</span>}<span class="sc">;</span></li><li>       <span class="s">}</span> elsif <span class="s">(</span> <span class="q">/^\s*##\s*DEBUG_BEGIN/</span> <span class="s">)</span> <span class="s">{</span></li><li>          <span class="i">$self</span>-&gt;{<span class="w">InTraceBlock</span>} = <span class="w">TRUE</span><span class="sc">;</span></li><li>       <span class="s">}</span></li><li>       <a class="l_k" href="functions/return.html">return</a> <span class="i">$status</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>Be warned: just as the C-preprocessor doesn't know C, the Debug filter
doesn't know Perl. It can be fooled quite easily:</p>
<pre class="verbatim"><ol><li>    print &lt;&lt;EOM;</li><li>    ##DEBUG_BEGIN</li><li>    EOM</li></ol></pre><p>Such things aside, you can see that a lot can be achieved with a modest
amount of code.</p>
<a name="CONCLUSION"></a><h1>CONCLUSION</h1>
<p>You now have better understanding of what a source filter is, and you
might even have a possible use for them. If you feel like playing with
source filters but need a bit of inspiration, here are some extra
features you could add to the Debug filter.</p>
<p>First, an easy one. Rather than having debugging code that is
all-or-nothing, it would be much more useful to be able to control
which specific blocks of debugging code get included. Try extending the
syntax for debug blocks to allow each to be identified. The contents of
the <code class="inline"><span class="w">DEBUG</span></code>
 environment variable can then be used to control which
blocks get included.</p>
<p>Once you can identify individual blocks, try allowing them to be
nested. That isn't difficult either.</p>
<p>Here is an interesting idea that doesn't involve the Debug filter.
Currently Perl subroutines have fairly limited support for formal
parameter lists. You can specify the number of parameters and their
type, but you still have to manually take them out of the <code class="inline"><span class="i">@_</span></code>
 array
yourself. Write a source filter that allows you to have a named
parameter list. Such a filter would turn this:</p>
<pre class="verbatim"><ol><li><a name="MySub"></a>    sub <span class="m">MySub ($first, $second, @rest)</span> <span class="s">{</span> ... <span class="s">}</span></li></ol></pre><p>into this:</p>
<pre class="verbatim"><ol><li><a name="MySub"></a>    sub <span class="m">MySub($$@)</span> <span class="s">{</span></li><li>       <a class="l_k" href="functions/my.html">my</a> <span class="s">(</span><span class="i">$first</span><span class="s">)</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>       <a class="l_k" href="functions/my.html">my</a> <span class="s">(</span><span class="i">$second</span><span class="s">)</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>       <a class="l_k" href="functions/my.html">my</a> <span class="s">(</span><span class="i">@rest</span><span class="s">)</span> = <span class="i">@_</span><span class="sc">;</span></li><li>       ...</li><li>    <span class="s">}</span></li></ol></pre><p>Finally, if you feel like a real challenge, have a go at writing a
full-blown Perl macro preprocessor as a source filter. Borrow the
useful features from the C preprocessor and any other macro processors
you know. The tricky bit will be choosing how much knowledge of Perl's
syntax you want your filter to have.</p>
<a name="LIMITATIONS"></a><h1>LIMITATIONS</h1>
<p>Source filters only work on the string level, thus are highly limited
in its ability to change source code on the fly. It cannot detect
comments, quoted strings, heredocs, it is no replacement for a real
parser.
The only stable usage for source filters are encryption, compression,
or the byteloader, to translate binary code back to source code.</p>
<p>See for example the limitations in <a href="http://search.cpan.org/perldoc/Switch">Switch</a>, which uses source filters,
and thus is does not work inside a string eval, the presence of
regexes with embedded newlines that are specified with raw <code class="inline"><span class="q">/.../</span></code>

delimiters and don't have a modifier <code class="inline">/x</code> are indistinguishable from
code chunks beginning with the division operator <code class="inline">/</code>. As a workaround
you must use <code class="inline"><a class="l_k" href="functions/m.html">m/.../</a></code> or <code class="inline"><a class="l_k" href="functions/m.html">m?...?</a></code> for such patterns.  See
<a href="http://search.cpan.org/perldoc?Switch#LIMITATIONS">http://search.cpan.org/perldoc?Switch#LIMITATIONS</a></p>
<p>Currently the content of the <code class="inline"><a name="__DATA__"></a>__DATA__</code>
 block is not filtered.</p>
<p>Currently internal buffer lengths are limited to 32-bit only.</p>
<a name="THINGS-TO-LOOK-OUT-FOR"></a><h1>THINGS TO LOOK OUT FOR</h1>
<ul>
<li><a name="Some-Filters-Clobber-the-DATA-Handle"></a><b>Some Filters Clobber the <code class="inline"><span class="w">DATA</span></code>
 Handle</b>
<p>Some source filters use the <code class="inline"><span class="w">DATA</span></code>
 handle to read the calling program.
When using these source filters you cannot rely on this handle, nor expect
any particular kind of behavior when operating on it.  Filters based on
Filter::Util::Call (and therefore Filter::Simple) do not alter the <code class="inline"><span class="w">DATA</span></code>

filehandle, but on the other hand totally ignore the text after <code class="inline"><a name="__DATA__"></a>__DATA__</code>
.</p>
</li>
</ul>
<a name="REQUIREMENTS"></a><h1>REQUIREMENTS</h1>
<p>The Source Filters distribution is available on CPAN, in</p>
<pre class="verbatim"><ol><li>    <span class="w">CPAN</span>/<span class="w">modules</span>/<span class="w">by</span>-<span class="w">module</span>/<span class="w">Filter</span></li></ol></pre><p>Starting from Perl 5.8 Filter::Util::Call (the core part of the
Source Filters distribution) is part of the standard Perl distribution.
Also included is a friendlier interface called Filter::Simple, by
Damian Conway.</p>
<a name="AUTHOR"></a><h1>AUTHOR</h1>
<p>Paul Marquess &lt;Paul.Marquess@btinternet.com&gt;</p>
<p>Reini Urban &lt;rurban@cpan.org&gt;</p>
<a name="Copyrights"></a><h1>Copyrights</h1>
<p>The first version of this article originally appeared in The Perl
Journal #11, and is copyright 1998 The Perl Journal. It appears
courtesy of Jon Orwant and The Perl Journal.  This document may be
distributed under the same terms as Perl itself.</p>




  <div id="page_index" class="hud_container">
    <div id="page_index_header" class="hud_header">
      <div id="page_index_close" class="hud_close"><a href="#" onClick="pageIndex.hide();return false;"></a></div>
      <div id="page_index_title" class="hud_title"><span class="hud_span_top">Page index</span></div>
      <div id="page_index_topright" class="hud_topright"></div>
    </div>
    <div id="page_index_content" class="hud_content">
      <ul><li><a href="#NAME">NAME</a><li><a href="#DESCRIPTION">DESCRIPTION</a><li><a href="#CONCEPTS">CONCEPTS</a><li><a href="#USING-FILTERS">USING FILTERS</a><li><a href="#WRITING-A-SOURCE-FILTER">WRITING A SOURCE FILTER</a><li><a href="#WRITING-A-SOURCE-FILTER-IN-C">WRITING A SOURCE FILTER IN C</a><li><a href="#CREATING-A-SOURCE-FILTER-AS-A-SEPARATE-EXECUTABLE">CREATING A SOURCE FILTER AS A SEPARATE EXECUTABLE</a><li><a href="#WRITING-A-SOURCE-FILTER-IN-PERL">WRITING A SOURCE FILTER IN PERL</a><li><a href="#USING-CONTEXT%3a-THE-DEBUG-FILTER">USING CONTEXT: THE DEBUG FILTER</a><li><a href="#CONCLUSION">CONCLUSION</a><li><a href="#LIMITATIONS">LIMITATIONS</a><li><a href="#THINGS-TO-LOOK-OUT-FOR">THINGS TO LOOK OUT FOR</a><li><a href="#REQUIREMENTS">REQUIREMENTS</a><li><a href="#AUTHOR">AUTHOR</a><li><a href="#Copyrights">Copyrights</a></ul>
    </div>
    <div id="page_index_footer" class="hud_footer">
      <div id="page_index_bottomleft" class="hud_bottomleft"></div>
      <div id="page_index_bottom" class="hud_bottom"><span class="hud_span_bottom"></span></div>
      <div id="page_index_resize" class="hud_resize"></div>
    </div>
  </div>


	    &nbsp;
          </div>
          <div id="content_footer">
          </div>
        </div>
        <div class="clear"></div>
      </div>
      
    <div id="footer">
      <div id="footer_content">
        <div id="footer_strapline">
          perldoc.perl.org - Official documentation for the Perl programming language
        </div>
        <div id="footer_links">
          <div id="address">
            <p class="name">Contact details</p>
            <p class="address">
	      Site maintained by <a href="mailto:jj@jonallen.info">Jon Allen (JJ)</a><br>
	    </p>
            <p class="contact">
              Documentation maintained by the <a href="http://lists.cpan.org/showlist.cgi?name=perl5-porters">Perl 5 Porters</a>
            </p>
          </div>
          <ul class="f1">
            <li>Manual
              <ul class="f2">
                <li><a href="index-overview.html">Overview</a>
                <li><a href="index-tutorials.html">Tutorials</a>
                <li><a href="index-faq.html">FAQs</a>
                <li><a href="index-history.html">Changes</a>
              </ul>
            <li>Reference
              <ul class="f2">
                <li><a href="index-language.html">Language</a>
                <li><a href="index-functions.html">Functions</a>
                <li><a href="perlop.html">Operators</a>
                <li><a href="perlvar.html">Variables</a>
              </ul>
            <li>Modules
              <ul class="f2">
                <li><a href="index-modules-A.html">Modules</a>
                <li><a href="index-pragmas.html">Pragmas</a>
                <li><a href="index-utilities.html">Utilities</a>
              </ul>
            <li>Misc
              <ul class="f2">
                <li><a href="index-licence.html">License</a>
                <li><a href="index-internals.html">Internals</a>
                <li><a href="index-platforms.html">Platforms</a>
              </ul>          </ul>
          <div class="clear"></div>
        </div>
      </div>
      <div id="footer_end">
      </div>
    </div>
      
    </div>
      <script language="JavaScript" type="text/javascript" src="static/exploreperl.js"></script>
      <script language="JavaScript" src="static/combined-20100403.js" type="text/javascript"></script>
<script language="JavaScript" type="text/javascript">
  perldoc.setPath(0);
  perldoc.pageName    = 'perlfilter';
  perldoc.pageAddress = 'perlfilter.html';
  perldoc.contentPage = 1;
  explorePerl.render();
  explorePerl.addEvents('explore_anchor');
</script>
    
  </body>
</html>