ActivePerl Documentation
|
NAMEFilter::Util::Call - Perl Source Filter Utility Module
SUPPORTED PLATFORMS
DESCRIPTIONThis module provides you with the framework to write Source Filters in Perl. A Perl Source Filter is implemented as a Perl module. The structure of the module can take one of two broadly similar formats. To distinguish between them, the first will be referred to as method filter and the second as closure filter. Here is a skeleton for the method filter:
package MyFilter ;
use Filter::Util::Call ;
sub import
{
my($type, @arguments) = @_ ;
filter_add([]) ;
}
sub filter
{
my($self) = @_ ;
my($status) ;
$status = filter_read() ;
$status ;
}
1 ;
and this is the equivalent skeleton for the closure filter:
package MyFilter ;
use Filter::Util::Call ;
sub import
{
my($type, @arguments) = @_ ;
filter_add(
sub
{
my($status) ;
$status = filter_read() ;
$status ;
} )
}
1 ;
To make use of either of the two filter modules above, place the line below in a Perl source file.
use MyFilter;
In fact, the skeleton modules shown above are fully functional Source Filters, albeit fairly useless ones. All they does is filter the source stream without modifying it at all. As you can see both modules have a broadly similar structure. They both
make use of the To make proper use of the closure filter shown above you need to have a good understanding of the concept of a closure. See the perlref manpage for more details on the mechanics of closures.
use Filter::Util::CallThe following functions are exported by
filter_add()
filter_read()
filter_read_exact()
filter_del()
import()The It will always have at least one parameter automatically passed by Perl
- this corresponds to the name of the package. In the example above it
will be Apart from the first parameter, import can accept an optional list of parameters. These can be used to pass parameters to the filter. For example:
use MyFilter qw(a b c) ;
will result in the
@_ [0] => "MyFilter"
@_ [1] => "a"
@_ [2] => "b"
@_ [3] => "c"
Before terminating, the filter_add() The function, If a CODE reference is used then a closure filter will be assumed. If a CODE reference is not used, a method filter will be assumed.
In a method filter, the reference can be used to store context
information. The reference will be blessed into the package by
See the filters at the end of this documents for examples of using context information using both method filters and closure filters.
filter() and anonymous subBoth the The big difference between the two types of filter is that the method filter uses the object passed to the method to store any context data, whereas the closure filter uses the lexical variables that are maintained by the closure. Note that the single parameter passed to the method filter,
Here is a list of the common features of the anonymous sub and the
EXAMPLESHere are a few examples which illustrate the key concepts - as such most of them are of little practical use. The
Example 1: A simple filter.Below is a method filter which is hard-wired to replace all
occurrences of the string
package Joe2Jim ;
use Filter::Util::Call ;
sub import
{
my($type) = @_ ;
filter_add(bless []) ;
}
sub filter
{
my($self) = @_ ;
my($status) ;
s/Joe/Jim/g
if ($status = filter_read()) > 0 ;
$status ;
}
1 ;
Here is an example of using the filter:
use Joe2Jim ;
print "Where is Joe?\n" ;
And this is what the script above will print:
Where is Jim?
Example 2: Using the contextThe previous example was not particularly useful. To make it more
general purpose we will make use of the context data and allow any
arbitrary from and to strings to be used. This time we will use a
closure filter. To reflect its enhanced role, the filter is called
package Subst ;
use Filter::Util::Call ;
use Carp ;
sub import
{
croak("usage: use Subst qw(from to)")
unless @_ == 3 ;
my ($self, $from, $to) = @_ ;
filter_add(
sub
{
my ($status) ;
s/$from/$to/
if ($status = filter_read()) > 0 ;
$status ;
})
}
1 ;
and is used like this:
use Subst qw(Joe Jim) ;
print "Where is Joe?\n" ;
Example 3: Using the context within the filterHere is a filter which a variation of the Once EOF is detected (
package Count ;
use Filter::Util::Call ;
sub filter
{
my ($self) = @_ ;
my ($status) ;
if (($status = filter_read()) > 0 ) {
s/Joe/Jim/g ;
++ $$self ;
}
elsif ($$self >= 0) { # EOF
$_ = "print q[Made ${$self} substitutions\n]" ;
$status = 1 ;
$$self = -1 ;
}
$status ;
}
sub import
{
my ($self) = @_ ;
my ($count) = 0 ;
filter_add(\$count) ;
}
1 ;
Here is a script which uses it:
use Count ;
print "Hello Joe\n" ;
print "Where is Joe\n" ;
Outputs:
Hello Jim
Where is Jim
Made 2 substitutions
Example 4: Using filter_delAnother variation on a theme. This time we will modify the
:/start/,/stop/s/from/to/
When used as a filter we want to invoke it like this:
use NewSubst qw(start stop from to) ;
Here is the module.
package NewSubst ;
use Filter::Util::Call ;
use Carp ;
sub import
{
my ($self, $start, $stop, $from, $to) = @_ ;
my ($found) = 0 ;
croak("usage: use Subst qw(start stop from to)")
unless @_ == 5 ;
filter_add(
sub
{
my ($status) ;
if (($status = filter_read()) > 0) {
$found = 1
if $found == 0 and /$start/ ;
if ($found) {
s/$from/$to/ ;
filter_del() if /$stop/ ;
}
}
$status ;
} )
}
1 ;
AUTHORPaul Marquess
DATE26th January 1996
|