📄 perlfilter.pod
字号:
1;All Perl source filters are implemented as Perl classes and have thesame basic structure as the example above.First, we include the C<Filter::Util::Call> module, which exports anumber of functions into your filter's namespace. The filter shownabove uses two of these functions, C<filter_add()> andC<filter_read()>.Next, we create the filter object and associate it with the sourcestream by defining the C<import> function. If you know Perl wellenough, you know that C<import> is called automatically every time amodule is included with a use statement. This makes C<import> the idealplace to both create and install a filter object.In the example filter, the object (C<$ref>) is blessed just like anyother Perl object. Our example uses an anonymous array, but this isn'ta requirement. Because this example doesn't need to store any contextinformation, we could have used a scalar or hash reference just aswell. The next section demonstrates context data.The association between the filter object and the source stream is madewith the C<filter_add()> function. This takes a filter object as aparameter (C<$ref> in this case) and installs it in the source stream.Finally, there is the code that actually does the filtering. For thistype of Perl source filter, all the filtering is done in a methodcalled C<filter()>. (It is also possible to write a Perl source filterusing a closure. See the C<Filter::Util::Call> manual page for moredetails.) It's called every time the Perl parser needs another line ofsource to process. The C<filter()> method, in turn, reads lines fromthe source stream using the C<filter_read()> function.If a line was available from the source stream, C<filter_read()>returns a status value greater than zero and appends the line to C<$_>.A status value of zero indicates end-of-file, less than zero means anerror. The filter function itself is expected to return its status inthe same way, and put the filtered line it wants written to the sourcestream in C<$_>. The use of C<$_> accounts for the brevity of most Perlsource filters.In order to make use of the rot13 filter we need some way of encodingthe source file in rot13 format. The script below, C<mkrot13>, doesjust that. die "usage mkrot13 filename\n" unless @ARGV; my $in = $ARGV[0]; my $out = "$in.tmp"; open(IN, "<$in") or die "Cannot open file $in: $!\n"; open(OUT, ">$out") or die "Cannot open file $out: $!\n"; print OUT "use Rot13;\n"; while (<IN>) { tr/a-zA-Z/n-za-mN-ZA-M/; print OUT; } close IN; close OUT; unlink $in; rename $out, $in;If we encrypt this with C<mkrot13>: print " hello fred \n";the result will be this: use Rot13; cevag "uryyb serq\a";Running it produces this output: hello fred=head1 USING CONTEXT: THE DEBUG FILTERThe rot13 example was a trivial example. Here's another demonstrationthat shows off a few more features.Say you wanted to include a lot of debugging code in your Perl scriptduring development, but you didn't want it available in the releasedproduct. Source filters offer a solution. In order to keep the examplesimple, let's say you wanted the debugging output to be controlled byan environment variable, C<DEBUG>. Debugging code is enabled if thevariable exists, otherwise it is disabled.Two special marker lines will bracket debugging code, like this: ## DEBUG_BEGIN if ($year > 1999) { warn "Debug: millennium bug in year $year\n"; } ## DEBUG_ENDWhen the C<DEBUG> environment variable exists, the filter ensures thatPerl parses only the code between the C<DEBUG_BEGIN> and C<DEBUG_END>markers. That means that when C<DEBUG> does exist, the code aboveshould be passed through the filter unchanged. The marker lines canalso be passed through as-is, because the Perl parser will see them ascomment lines. When C<DEBUG> isn't set, we need a way to disable thedebug code. A simple way to achieve that is to convert the linesbetween the two markers into comments: ## DEBUG_BEGIN #if ($year > 1999) { # warn "Debug: millennium bug in year $year\n"; #} ## DEBUG_ENDHere is the complete Debug filter: package Debug; use strict; use warnings; use Filter::Util::Call; use constant TRUE => 1; use constant FALSE => 0; sub import { my ($type) = @_; my (%context) = ( Enabled => defined $ENV{DEBUG}, InTraceBlock => FALSE, Filename => (caller)[1], LineNo => 0, LastBegin => 0, ); filter_add(bless \%context); } sub Die { my ($self) = shift; my ($message) = shift; my ($line_no) = shift || $self->{LastBegin}; die "$message at $self->{Filename} line $line_no.\n" } sub filter { my ($self) = @_; my ($status); $status = filter_read(); ++ $self->{LineNo}; # deal with EOF/error first if ($status <= 0) { $self->Die("DEBUG_BEGIN has no DEBUG_END") if $self->{InTraceBlock}; return $status; } if ($self->{InTraceBlock}) { if (/^\s*##\s*DEBUG_BEGIN/ ) { $self->Die("Nested DEBUG_BEGIN", $self->{LineNo}) } elsif (/^\s*##\s*DEBUG_END/) { $self->{InTraceBlock} = FALSE; } # comment out the debug lines when the filter is disabled s/^/#/ if ! $self->{Enabled}; } elsif ( /^\s*##\s*DEBUG_BEGIN/ ) { $self->{InTraceBlock} = TRUE; $self->{LastBegin} = $self->{LineNo}; } elsif ( /^\s*##\s*DEBUG_END/ ) { $self->Die("DEBUG_END has no DEBUG_BEGIN", $self->{LineNo}); } return $status; } 1;The big difference between this filter and the previous example is theuse of context data in the filter object. The filter object is based ona hash reference, and is used to keep various pieces of contextinformation between calls to the filter function. All but two of thehash fields are used for error reporting. The first of those two,Enabled, is used by the filter to determine whether the debugging codeshould be given to the Perl parser. The second, InTraceBlock, is truewhen the filter has encountered a C<DEBUG_BEGIN> line, but has not yetencountered the following C<DEBUG_END> line.If you ignore all the error checking that most of the code does, theessence of the filter is as follows: sub filter { my ($self) = @_; my ($status); $status = filter_read(); # deal with EOF/error first return $status if $status <= 0; if ($self->{InTraceBlock}) { if (/^\s*##\s*DEBUG_END/) { $self->{InTraceBlock} = FALSE } # comment out debug lines when the filter is disabled s/^/#/ if ! $self->{Enabled}; } elsif ( /^\s*##\s*DEBUG_BEGIN/ ) { $self->{InTraceBlock} = TRUE; } return $status; }Be warned: just as the C-preprocessor doesn't know C, the Debug filterdoesn't know Perl. It can be fooled quite easily: print <<EOM; ##DEBUG_BEGIN EOMSuch things aside, you can see that a lot can be achieved with a modestamount of code.=head1 CONCLUSIONYou now have better understanding of what a source filter is, and youmight even have a possible use for them. If you feel like playing withsource filters but need a bit of inspiration, here are some extrafeatures you could add to the Debug filter.First, an easy one. Rather than having debugging code that isall-or-nothing, it would be much more useful to be able to controlwhich specific blocks of debugging code get included. Try extending thesyntax for debug blocks to allow each to be identified. The contents ofthe C<DEBUG> environment variable can then be used to control whichblocks get included.Once you can identify individual blocks, try allowing them to benested. That isn't difficult either.Here is an interesting idea that doesn't involve the Debug filter.Currently Perl subroutines have fairly limited support for formalparameter lists. You can specify the number of parameters and theirtype, but you still have to manually take them out of the C<@_> arrayyourself. Write a source filter that allows you to have a namedparameter list. Such a filter would turn this: sub MySub ($first, $second, @rest) { ... }into this: sub MySub($$@) { my ($first) = shift; my ($second) = shift; my (@rest) = @_; ... }Finally, if you feel like a real challenge, have a go at writing afull-blown Perl macro preprocessor as a source filter. Borrow theuseful features from the C preprocessor and any other macro processorsyou know. The tricky bit will be choosing how much knowledge of Perl'ssyntax you want your filter to have.=head1 THINGS TO LOOK OUT FOR=over 5=item Some Filters Clobber the C<DATA> HandleSome source filters use the C<DATA> handle to read the calling program.When using these source filters you cannot rely on this handle, nor expectany particular kind of behavior when operating on it. Filters based onFilter::Util::Call (and therefore Filter::Simple) do not alter the C<DATA>filehandle.=back=head1 REQUIREMENTSThe Source Filters distribution is available on CPAN, in CPAN/modules/by-module/FilterStarting from Perl 5.8 Filter::Util::Call (the core part of theSource Filters distribution) is part of the standard Perl distribution.Also included is a friendlier interface called Filter::Simple, byDamian Conway.=head1 AUTHORPaul Marquess E<lt>Paul.Marquess@btinternet.comE<gt>=head1 CopyrightsThis article originally appeared in The Perl Journal #11, and iscopyright 1998 The Perl Journal. It appears courtesy of Jon Orwant andThe Perl Journal. This document may be distributed under the same termsas Perl itself.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -