📄 perlfilter.1
字号:
.\" Automatically generated by Pod::Man 2.16 (Pod::Simple 3.05).\".\" Standard preamble:.\" ========================================================================.de Sh \" Subsection heading.br.if t .Sp.ne 5.PP\fB\\$1\fR.PP...de Sp \" Vertical space (when we can't use .PP).if t .sp .5v.if n .sp...de Vb \" Begin verbatim text.ft CW.nf.ne \\$1...de Ve \" End verbatim text.ft R.fi...\" Set up some character translations and predefined strings. \*(-- will.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left.\" double quote, and \*(R" will give a right double quote. \*(C+ will.\" give a nicer C++. Capital omega is used to do unbreakable dashes and.\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff,.\" nothing in troff, for use with C<>..tr \(*W-.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'.ie n \{\. ds -- \(*W-. ds PI pi. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch. ds L" "". ds R" "". ds C` "". ds C' ""'br\}.el\{\. ds -- \|\(em\|. ds PI \(*p. ds L" ``. ds R" '''br\}.\".\" Escape single quotes in literal strings from groff's Unicode transform..ie \n(.g .ds Aq \(aq.el .ds Aq '.\".\" If the F register is turned on, we'll generate index entries on stderr for.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index.\" entries marked with X<> in POD. Of course, you'll have to process the.\" output yourself in some meaningful fashion..ie \nF \{\. de IX. tm Index:\\$1\t\\n%\t"\\$2"... nr % 0. rr F.\}.el \{\. de IX...\}.\".\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2)..\" Fear. Run. Save yourself. No user-serviceable parts.. \" fudge factors for nroff and troff.if n \{\. ds #H 0. ds #V .8m. ds #F .3m. ds #[ \f1. ds #] \fP.\}.if t \{\. ds #H ((1u-(\\\\n(.fu%2u))*.13m). ds #V .6m. ds #F 0. ds #[ \&. ds #] \&.\}. \" simple accents for nroff and troff.if n \{\. ds ' \&. ds ` \&. ds ^ \&. ds , \&. ds ~ ~. ds /.\}.if t \{\. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u". ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'.\}. \" troff and (daisy-wheel) nroff accents.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'.ds 8 \h'\*(#H'\(*b\h'-\*(#H'.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#].ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#].ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#].ds ae a\h'-(\w'a'u*4/10)'e.ds Ae A\h'-(\w'A'u*4/10)'E. \" corrections for vroff.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'. \" for low resolution devices (crt and lpr).if \n(.H>23 .if \n(.V>19 \\{\. ds : e. ds 8 ss. ds o a. ds d- d\h'-1'\(ga. ds D- D\h'-1'\(hy. ds th \o'bp'. ds Th \o'LP'. ds ae ae. ds Ae AE.\}.rm #[ #] #H #V #F C.\" ========================================================================.\".IX Title "PERLFILTER 1".TH PERLFILTER 1 "2007-12-18" "perl v5.10.0" "Perl Programmers Reference Guide".\" For nroff, turn off justification. Always turn off hyphenation; it makes.\" way too many mistakes in technical documents..if n .ad l.nh.SH "NAME"perlfilter \- Source Filters.SH "DESCRIPTION".IX Header "DESCRIPTION"This article is about a little-known feature of Perl called\&\fIsource filters\fR. Source filters alter the program text of a modulebefore Perl sees it, much as a C preprocessor alters the source text ofa C program before the compiler sees it. This article tells you moreabout what source filters are, how they work, and how to write yourown..PPThe original purpose of source filters was to let you encrypt yourprogram source to prevent casual piracy. This isn't all they can do, asyou'll soon learn. But first, the basics..SH "CONCEPTS".IX Header "CONCEPTS"Before the Perl interpreter can execute a Perl script, it must firstread it from a file into memory for parsing and compilation. If thatscript itself includes other scripts with a \f(CW\*(C`use\*(C'\fR or \f(CW\*(C`require\*(C'\fRstatement, then each of those scripts will have to be read from theirrespective files as well..PPNow think of each logical connection between the Perl parser and anindividual file as a \fIsource stream\fR. A source stream is created whenthe Perl parser opens a file, it continues to exist as the source codeis read into memory, and it is destroyed when Perl is finished parsingthe file. If the parser encounters a \f(CW\*(C`require\*(C'\fR or \f(CW\*(C`use\*(C'\fR statement ina source stream, a new and distinct stream is created just for thatfile..PPThe diagram below represents a single source stream, with the flow ofsource from a Perl script file on the left into the Perl parser on theright. This is how Perl normally operates..PP.Vb 1\& file \-\-\-\-\-\-\-> parser.Ve.PPThere are two important points to remember:.IP "1." 5Although there can be any number of source streams in existence at anygiven time, only one will be active..IP "2." 5Every source stream is associated with only one file..PPA source filter is a special kind of Perl module that intercepts andmodifies a source stream before it reaches the parser. A source filterchanges our diagram like this:.PP.Vb 1\& file \-\-\-\-> filter \-\-\-\-> parser.Ve.PPIf that doesn't make much sense, consider the analogy of a commandpipeline. Say you have a shell script stored in the compressed file\&\fItrial.gz\fR. The simple pipeline command below runs the script withoutneeding to create a temporary file to hold the uncompressed file..PP.Vb 1\& gunzip \-c trial.gz | sh.Ve.PPIn this case, the data flow from the pipeline can be represented as follows:.PP.Vb 1\& trial.gz \-\-\-\-> gunzip \-\-\-\-> sh.Ve.PPWith source filters, you can store the text of your script compressed and use a source filter to uncompress it for Perl's parser:.PP.Vb 2\& compressed gunzip\& Perl program \-\-\-> source filter \-\-\-> parser.Ve.SH "USING FILTERS".IX Header "USING FILTERS"So how do you use a source filter in a Perl script? Above, I said thata source filter is just a special kind of module. Like all Perlmodules, a source filter is invoked with a use statement..PPSay you want to pass your Perl source through the C preprocessor beforeexecution. You could use the existing \f(CW\*(C`\-P\*(C'\fR command line option to dothis, but as it happens, the source filters distribution comes with a Cpreprocessor filter module called Filter::cpp. Let's use that instead..PPBelow is an example program, \f(CW\*(C`cpp_test\*(C'\fR, which makes use of this filter.Line numbers have been added to allow specific lines to be referencedeasily..PP.Vb 4\& 1: use Filter::cpp;\& 2: #define TRUE 1\& 3: $a = TRUE;\& 4: print "a = $a\en";.Ve.PPWhen you execute this script, Perl creates a source stream for thefile. Before the parser processes any of the lines from the file, thesource stream looks like this:.PP.Vb 1\& cpp_test \-\-\-\-\-\-\-\-\-> parser.Ve.PPLine 1, \f(CW\*(C`use Filter::cpp\*(C'\fR, includes and installs the \f(CW\*(C`cpp\*(C'\fR filtermodule. All source filters work this way. The use statement is compiledand executed at compile time, before any more of the file is read, andit attaches the cpp filter to the source stream behind the scenes. Nowthe data flow looks like this:.PP.Vb 1\& cpp_test \-\-\-\-> cpp filter \-\-\-\-> parser.Ve.PPAs the parser reads the second and subsequent lines from the sourcestream, it feeds those lines through the \f(CW\*(C`cpp\*(C'\fR source filter beforeprocessing them. The \f(CW\*(C`cpp\*(C'\fR filter simply passes each line through thereal C preprocessor. The output from the C preprocessor is theninserted back into the source stream by the filter..PP.Vb 5\& .\-> cpp \-\-.\& | |\& | |\& | <\-\*(Aq\& cpp_test \-\-\-\-> cpp filter \-\-\-\-> parser.Ve.PPThe parser then sees the following code:.PP.Vb 3\& use Filter::cpp;\& $a = 1;\& print "a = $a\en";.Ve.PPLet's consider what happens when the filtered code includes anothermodule with use:.PP.Vb 5\& 1: use Filter::cpp;\& 2: #define TRUE 1\& 3: use Fred;\& 4: $a = TRUE;\& 5: print "a = $a\en";.Ve.PPThe \f(CW\*(C`cpp\*(C'\fR filter does not apply to the text of the Fred module, onlyto the text of the file that used it (\f(CW\*(C`cpp_test\*(C'\fR). Although the usestatement on line 3 will pass through the cpp filter, the module thatgets included (\f(CW\*(C`Fred\*(C'\fR) will not. The source streams look like thisafter line 3 has been parsed and before line 4 is parsed:.PP.Vb 1\& cpp_test \-\-\-> cpp filter \-\-\-> parser (INACTIVE)\&\& Fred.pm \-\-\-\-> parser.Ve.PPAs you can see, a new stream has been created for reading the sourcefrom \f(CW\*(C`Fred.pm\*(C'\fR. This stream will remain active until all of \f(CW\*(C`Fred.pm\*(C'\fRhas been parsed. The source stream for \f(CW\*(C`cpp_test\*(C'\fR will still exist,but is inactive. Once the parser has finished reading Fred.pm, thesource stream associated with it will be destroyed. The source streamfor \f(CW\*(C`cpp_test\*(C'\fR then becomes active again and the parser reads line 4and subsequent lines from \f(CW\*(C`cpp_test\*(C'\fR..PPYou 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..PPFor example, if you have a uuencoded and compressed source file, it ispossible to stack a uudecode filter and an uncompression filter likethis:.PP.Vb 4\& use Filter::uudecode; use Filter::uncompress;\& M\*(AqXL(".H<US4\*(Aq\*(AqV9I;F%L\*(Aq)Q;>7/;1I;_>_I3=&E=%:F*I"T?22Q/\& M6]9*<IQCO*XFT"0[PL%%\*(AqY+IG?WN^ZYN\-$\*(AqJ.[.JE$,20/?K=_[>\& ....Ve.PPOnce the first line has been processed, the flow will look like this:.PP.Vb 2\& file \-\-\-> uudecode \-\-\-> uncompress \-\-\-> parser\& filter filter.Ve.PPData flows through filters in the same order they appear in the sourcefile. The uudecode filter appeared before the uncompress filter, so thesource file will be uudecoded before it's uncompressed..SH "WRITING A SOURCE FILTER".IX Header "WRITING A SOURCE FILTER"There are three ways to write your own source filter. You can write itin 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 outof the way first. Writing the filter in Perl is most convenient, soI'll devote the most space to it..SH "WRITING A SOURCE FILTER IN C".IX Header "WRITING A SOURCE FILTER IN C"The first of the three available techniques is to write the filtercompletely in C. The external module you create interfaces directlywith the source filter hooks provided by Perl..PPThe advantage of this technique is that you have complete control overthe implementation of your filter. The big disadvantage is theincreased complexity required to write the filter \- not only do youneed to understand the source filter hooks, but you also need areasonable knowledge of Perl guts. One of the few times it is worthgoing to this trouble is when writing a source scrambler. The\&\f(CW\*(C`decrypt\*(C'\fR filter (which unscrambles the source before Perl parses it)included with the source filter distribution is an example of a Csource filter (see Decryption Filters, below)..IP "\fBDecryption Filters\fR" 5.IX Item "Decryption Filters"All decryption filters work on the principle of \*(L"security throughobscurity.\*(R" Regardless of how well you write a decryption filter andhow strong your encryption algorithm, anyone determined enough canretrieve the original source code. The reason is quite simple \- oncethe decryption filter has decrypted the source back to its originalform, fragments of it will be stored in the computer's memory as Perlparses it. The source might only be in memory for a short period oftime, but anyone possessing a debugger, skill, and lots of patience caneventually reconstruct your program..SpThat said, there are a number of steps that can be taken to make lifedifficult for the potential cracker. The most important: Write yourdecryption filter in C and statically link the decryption module intothe Perl binary. For further tips to make life difficult for thepotential cracker, see the file \fIdecrypt.pm\fR in the source filtersmodule..SH "CREATING A SOURCE FILTER AS A SEPARATE EXECUTABLE".IX Header "CREATING A SOURCE FILTER AS A SEPARATE EXECUTABLE"An alternative to writing the filter in C is to create a separateexecutable in the language of your choice. The separate executablereads from standard input, does whatever processing is necessary, andwrites the filtered data to standard output. \f(CW\*(C`Filter:cpp\*(C'\fR is anexample of a source filter implemented as a separate executable \- the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -