📄 filter::simple.3
字号:
\& FILTER {\& s/BANG\es+BANG/die \*(AqBANG\*(Aq if \e$BANG/g;\& }\& ""; # or: 0.Ve.PPor:.PP.Vb 4\& FILTER {\& s/BANG\es+BANG/die \*(AqBANG\*(Aq if \e$BANG/g;\& }\& { terminator => "" };.Ve.PP\&\fBNote that, no matter what you set the terminator pattern to,the actual terminator itself \f(BImust\fB be contained on a single source line.\fR.Sh "All-in-one interface".IX Subsection "All-in-one interface"Separating the loading of Filter::Simple:.PP.Vb 1\& use Filter::Simple;.Ve.PPfrom the setting up of the filtering:.PP.Vb 1\& FILTER { ... };.Ve.PPis useful because it allows other code (typically parser support codeor caching variables) to be defined before the filter is invoked.However, there is often no need for such a separation..PPIn those cases, it is easier to just append the filtering subroutine andany terminator specification directly to the \f(CW\*(C`use\*(C'\fR statement that loadsFilter::Simple, like so:.PP.Vb 3\& use Filter::Simple sub {\& s/BANG\es+BANG/die \*(AqBANG\*(Aq if \e$BANG/g;\& };.Ve.PPThis is exactly the same as:.PP.Vb 6\& use Filter::Simple;\& BEGIN {\& Filter::Simple::FILTER {\& s/BANG\es+BANG/die \*(AqBANG\*(Aq if \e$BANG/g;\& };\& }.Ve.PPexcept that the \f(CW\*(C`FILTER\*(C'\fR subroutine is not exported by Filter::Simple..Sh "Filtering only specific components of source code".IX Subsection "Filtering only specific components of source code"One of the problems with a filter like:.PP.Vb 1\& use Filter::Simple;\&\& FILTER { s/BANG\es+BANG/die \*(AqBANG\*(Aq if \e$BANG/g };.Ve.PPis that it indiscriminately applies the specified transformation tothe entire text of your source program. So something like:.PP.Vb 2\& warn \*(AqBANG BANG, YOU\*(AqRE DEAD\*(Aq;\& BANG BANG;.Ve.PPwill become:.PP.Vb 2\& warn \*(Aqdie \*(AqBANG\*(Aq if $BANG, YOU\*(AqRE DEAD\*(Aq;\& die \*(AqBANG\*(Aq if $BANG;.Ve.PPIt is very common when filtering source to only want to apply the filterto the non-character-string parts of the code, or alternatively to \fIonly\fRthe character strings..PPFilter::Simple supports this type of filtering by automaticallyexporting the \f(CW\*(C`FILTER_ONLY\*(C'\fR subroutine..PP\&\f(CW\*(C`FILTER_ONLY\*(C'\fR takes a sequence of specifiers that install separate(and possibly multiple) filters that act on only parts of the source code.For example:.PP.Vb 1\& use Filter::Simple;\&\& FILTER_ONLY\& code => sub { s/BANG\es+BANG/die \*(AqBANG\*(Aq if \e$BANG/g },\& quotelike => sub { s/BANG\es+BANG/CHITTY CHITTY/g };.Ve.PPThe \f(CW"code"\fR subroutine will only be used to filter parts of the sourcecode that are not quotelikes, \s-1POD\s0, or \f(CW\*(C`_\|_DATA_\|_\*(C'\fR. The \f(CW\*(C`quotelike\*(C'\fRsubroutine only filters Perl quotelikes (including here documents)..PPThe full list of alternatives is:.ie n .IP """code""" 4.el .IP "\f(CW``code''\fR" 4.IX Item """code"""Filters only those sections of the source code that are not quotelikes, \s-1POD\s0, or\&\f(CW\*(C`_\|_DATA_\|_\*(C'\fR..ie n .IP """code_no_comments""" 4.el .IP "\f(CW``code_no_comments''\fR" 4.IX Item """code_no_comments"""Filters only those sections of the source code that are not quotelikes, \s-1POD\s0,comments, or \f(CW\*(C`_\|_DATA_\|_\*(C'\fR..ie n .IP """executable""" 4.el .IP "\f(CW``executable''\fR" 4.IX Item """executable"""Filters only those sections of the source code that are not \s-1POD\s0 or \f(CW\*(C`_\|_DATA_\|_\*(C'\fR..ie n .IP """executable_no_comments""" 4.el .IP "\f(CW``executable_no_comments''\fR" 4.IX Item """executable_no_comments"""Filters only those sections of the source code that are not \s-1POD\s0, comments, or \f(CW\*(C`_\|_DATA_\|_\*(C'\fR..ie n .IP """quotelike""" 4.el .IP "\f(CW``quotelike''\fR" 4.IX Item """quotelike"""Filters only Perl quotelikes (as interpreted by\&\f(CW&Text::Balanced::extract_quotelike\fR)..ie n .IP """string""" 4.el .IP "\f(CW``string''\fR" 4.IX Item """string"""Filters only the string literal parts of a Perl quotelike (i.e. the contents of a string literal, either half of a \f(CW\*(C`tr///\*(C'\fR, the secondhalf of an \f(CW\*(C`s///\*(C'\fR)..ie n .IP """regex""" 4.el .IP "\f(CW``regex''\fR" 4.IX Item """regex"""Filters only the pattern literal parts of a Perl quotelike (i.e. the contents of a \f(CW\*(C`qr//\*(C'\fR or an \f(CW\*(C`m//\*(C'\fR, the first half of an \f(CW\*(C`s///\*(C'\fR)..ie n .IP """all""" 4.el .IP "\f(CW``all''\fR" 4.IX Item """all"""Filters everything. Identical in effect to \f(CW\*(C`FILTER\*(C'\fR..PPExcept for \f(CW\*(C`FILTER_ONLY code => sub {...}\*(C'\fR, each ofthe component filters is called repeatedly, once for each componentfound in the source code..PPNote that you can also apply two or more of the same type of filter ina single \f(CW\*(C`FILTER_ONLY\*(C'\fR. For example, here's a simple macro-preprocessor that is only applied within regexes,with a final debugging pass that prints the resulting source code:.PP.Vb 6\& use Regexp::Common;\& FILTER_ONLY\& regex => sub { s/!\e[/[^/g },\& regex => sub { s/%d/$RE{num}{int}/g },\& regex => sub { s/%f/$RE{num}{real}/g },\& all => sub { print if $::DEBUG };.Ve.Sh "Filtering only the code parts of source code".IX Subsection "Filtering only the code parts of source code"Most source code ceases to be grammatically correct when it is broken upinto the pieces between string literals and regexes. So the \f(CW\*(Aqcode\*(Aq\fRand \f(CW\*(Aqcode_no_comments\*(Aq\fR component filter behave slightly differentlyfrom the other partial filters described in the previous section..PPRather than calling the specified processor on each individual piece ofcode (i.e. on the bits between quotelikes), the \f(CW\*(Aqcode...\*(Aq\fR partialfilters operate on the entire source code, but with the quotelike bits(and, in the case of \f(CW\*(Aqcode_no_comments\*(Aq\fR, the comments) \*(L"blanked out\*(R"..PPThat is, a \f(CW\*(Aqcode...\*(Aq\fR filter \fIreplaces\fR each quoted string, quotelike,regex, \s-1POD\s0, and _\|_DATA_\|_ section with a placeholder. Thedelimiters of this placeholder are the contents of the \f(CW$;\fR variableat the time the filter is applied (normally \f(CW"\e034"\fR). The remainingfour bytes are a unique identifier for the component being replaced..PPThis approach makes it comparatively easy to write code preprocessorswithout worrying about the form or contents of strings, regexes, etc..PPFor convenience, during a \f(CW\*(Aqcode...\*(Aq\fR filtering operation, Filter::Simpleprovides a package variable (\f(CW$Filter::Simple::placeholder\fR) thatcontains a pre-compiled regex that matches any placeholder...andcaptures the identifier within the placeholder. Placeholders can bemoved and re-ordered within the source code as needed..PPIn addition, a second package variable (\f(CW@Filter::Simple::components\fR)contains a list of the various pieces of \f(CW$_\fR, as they were originally splitup to allow placeholders to be inserted..PPOnce the filtering has been applied, the original strings, regexes, \s-1POD\s0,etc. are re-inserted into the code, by replacing each placeholder withthe corresponding original component (from \f(CW@components\fR). Note thatthis means that the \f(CW@components\fR variable must be treated with extremecare within the filter. The \f(CW@components\fR array stores the \*(L"back\-translations\*(R" of each placeholder inserted into \f(CW$_\fR, as well as theinterstitial source code between placeholders. If the placeholderbacktranslations are altered in \f(CW@components\fR, they will be similarlychanged when the placeholders are removed from \f(CW$_\fR after the filteris complete..PPFor example, the following filter detects concatenated pairs ofstrings/quotelikes and reverses the order in which they areconcatenated:.PP.Vb 2\& package DemoRevCat;\& use Filter::Simple;\&\& FILTER_ONLY code => sub {\& my $ph = $Filter::Simple::placeholder;\& s{ ($ph) \es* [.] \es* ($ph) }{ $2.$1 }gx\& };.Ve.PPThus, the following code:.PP.Vb 1\& use DemoRevCat;\&\& my $str = "abc" . q(def);\&\& print "$str\en";.Ve.PPwould become:.PP.Vb 1\& my $str = q(def)."abc";\&\& print "$str\en";.Ve.PPand hence print:.PP.Vb 1\& defabc.Ve.ie n .Sh "Using Filter::Simple with an explicit ""import"" subroutine".el .Sh "Using Filter::Simple with an explicit \f(CWimport\fP subroutine".IX Subsection "Using Filter::Simple with an explicit import subroutine"Filter::Simple generates a special \f(CW\*(C`import\*(C'\fR subroutine foryour module (see \*(L"How it works\*(R") which would normally replace any\&\f(CW\*(C`import\*(C'\fR subroutine you might have explicitly declared..PPHowever, Filter::Simple is smart enough to notice your existing\&\f(CW\*(C`import\*(C'\fR and Do The Right Thing with it.That is, if you explicitly define an \f(CW\*(C`import\*(C'\fR subroutine in a packagethat's using Filter::Simple, that \f(CW\*(C`import\*(C'\fR subroutine will stillbe invoked immediately after any filter you install..PPThe only thing you have to remember is that the \f(CW\*(C`import\*(C'\fR subroutine\&\fImust\fR be declared \fIbefore\fR the filter is installed. If you use \f(CW\*(C`FILTER\*(C'\fRto install the filter:.PP.Vb 1\& package Filter::TurnItUpTo11;\&\& use Filter::Simple;\&\& FILTER { s/(\ew+)/\eU$1/ };.Ve.PPthat will almost never be a problem, but if you install a filteringsubroutine by passing it directly to the \f(CW\*(C`use Filter::Simple\*(C'\fRstatement:.PP.Vb 1\& package Filter::TurnItUpTo11;\&\& use Filter::Simple sub{ s/(\ew+)/\eU$1/ };.Ve.PPthen you must make sure that your \f(CW\*(C`import\*(C'\fR subroutine appears beforethat \f(CW\*(C`use\*(C'\fR statement..Sh "Using Filter::Simple and Exporter together".IX Subsection "Using Filter::Simple and Exporter together"Likewise, Filter::Simple is also smart enoughto Do The Right Thing if you use Exporter:.PP.Vb 3\& package Switch;\& use base Exporter;\& use Filter::Simple;\&\& @EXPORT = qw(switch case);\& @EXPORT_OK = qw(given when);\&\& FILTER { $_ = magic_Perl_filter($_) }.Ve.PPImmediately after the filter has been applied to the source,Filter::Simple will pass control to Exporter, so it can do its magic too..PPOf course, here too, Filter::Simple has to know you're using Exporterbefore it applies the filter. That's almost never a problem, but if you'renervous about it, you can guarantee that things will work correctly byensuring that your \f(CW\*(C`use base Exporter\*(C'\fR always precedes your\&\f(CW\*(C`use Filter::Simple\*(C'\fR..Sh "How it works".IX Subsection "How it works"The Filter::Simple module exports into the package that calls \f(CW\*(C`FILTER\*(C'\fR(or \f(CW\*(C`use\*(C'\fRs it directly) \*(-- such as package \*(L"\s-1BANG\s0\*(R" in the above example \*(--two automagically constructedsubroutines \*(-- \f(CW\*(C`import\*(C'\fR and \f(CW\*(C`unimport\*(C'\fR \*(-- which take care of all thenasty details..PPIn addition, the generated \f(CW\*(C`import\*(C'\fR subroutine passes its own argumentlist to the filtering subroutine, so the \s-1BANG\s0.pm filter could easily be made parametric:.PP.Vb 1\& package BANG;\& \& use Filter::Simple;\& \& FILTER {\& my ($die_msg, $var_name) = @_;\& s/BANG\es+BANG/die \*(Aq$die_msg\*(Aq if \e${$var_name}/g;\& };\&\& # and in some user code:\&\& use BANG "BOOM", "BAM"; # "BANG BANG" becomes: die \*(AqBOOM\*(Aq if $BAM.Ve.PPThe specified filtering subroutine is called every time a \f(CW\*(C`use BANG\*(C'\fR isencountered, and passed all the source code following that call, up toeither the next \f(CW\*(C`no BANG;\*(C'\fR (or whatever terminator you've set) or theend of the source file, whichever occurs first. By default, any \f(CW\*(C`noBANG;\*(C'\fR call must appear by itself on a separate line, or it is ignored..SH "AUTHOR".IX Header "AUTHOR"Damian Conway (damian@conway.org).SH "COPYRIGHT".IX Header "COPYRIGHT".Vb 3\& Copyright (c) 2000\-2001, Damian Conway. All Rights Reserved.\& This module is free software. It may be used, redistributed\& and/or modified under the same terms as Perl itself..Ve
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -