> This and other RFCs are available on the web at
>   http://dev.perl.org/rfc/
>
> =head1 TITLE
>
> Provide a standard module to simplify the creation of source filters
>
> =head1 VERSION
>
>   Maintainer: Damian Conway <[EMAIL PROTECTED]>
>   Date: 20 September 2000
>   Mailing List: [EMAIL PROTECTED]
>   Number: 264
>   Version: 1
>   Status: Developing
>
> =head1 ABSTRACT
>
> This RFC proposes that the interface to Perl's source filtering facilities
> be made much easier to use.
>
> =head1 DESCRIPTION
>
> Source filtering is an immensely powerful feature of recent
> versions of Perl.
> It allows one to extend the language itself (e.g. the Switch module), to
> simplify the language (e.g. Language::Pythonesque), or to
> completely recast the
> language (e.g. Lingua::Romana::Perligata). Effectively, it allows
> one to use
> the full power of Perl as its own, recursively applied, macro language.
>
> The Filter::Util::Call module (by Paul Marquess) provides a usable Perl
> interface to source filtering, but it is not nearly as simple as
> it could be.
>
> To use the module it is necessary to do the following:
>
> =over 4
>
> =item 1.
>
> Download, build, and install the Filter::Util::Call module.
>
> =item 2.
>
> Set up a module that does a C<use Filter::Util::Call>.
>
> =item 3.
>
> Within that module, create an C<import> subroutine.
>
> =item 4.
>
> Within the C<import> subroutine do a call to C<filter_add>, passing
> it either a subroutine reference.
>
> =item 5.
>
> Within the subroutine reference, call C<filter_read> or
> C<filter_read_exact>
> to "prime" $_ with source code data from the source file that will
> C<use> your module. Check the status value returned to see if any
> source code was actually read in.
>
> =item 6.
>
> Process the contents of $_ to change the source code in the
> desired manner.
>
> =item 7.
>
> Return the status value.
>
> =item 8.
>
> If the act of unimporting your module (via a C<no>) should cause source
> code filtering to cease, create an C<unimport> subroutine, and
> have it call
> C<filter_del>. Make sure that the call to C<filter_read> or
> C<filter_read_exact> in step 5 will not accidentally read past the
> C<no>. Effectively this limits source code filters to line-by-line
> operation, unless the C<import> subroutine does some fancy
> pre-pre-parsing of the source code it's filtering.
>
> =back
>
> For example, here is a minimal source code filter in a module named
> BANG.pm. It simply converts every occurrence of the sequence
> C<BANG\s+BANG>
> to the sequence C<die 'BANG' if $BANG> in any piece of code following a
> C<use BANG;> statement (until the next C<no BANG;> statement, if any):
>
>         package BANG;
>
>       use Filter::Util::Call ;
>
>       sub import {
>           filter_add( sub {
>               my $caller = caller;
>               my ($status, $no_seen, $data);
>               while ($status = filter_read()) {
>                       if (/^\s*no\s+$caller\s*;\s*$/) {
>                               $no_seen=1;
>                               last;
>                       }
>                       $data .= $_;
>                       $_ = "";
>               }
>               $_ = $data;
>               s/BANG\s+BANG/die 'BANG' if \$BANG/g
>                       unless $status < 0;
>               $_ .= "no $class;\n" if $no_seen;
>               return 1;
>           })
>       }
>
>       sub unimport {
>           filter_del();
>       }
>
>       1 ;
>
> Given this level of complexity, it's perhaps not surprising that source
> code filtering is not commonly used.

Whilst I don't have any problems with you module, I think you are
overstating the complexity of the existing situation. This should be all
that is needed to create your filter.

    package BANG;

    use Filter::Util::Call ;

    sub import
    {
      filter_add( sub {
        my ($status);
        s/BANG\s+/die 'BANG' if \$BANG/g
            if ($status = filter_read()) > 0 ;
        $status ;
      })
    }

    sub unimport
    {
        filter_del() ;
    }
    1 ;



> This RFC proposes that a new standard module -- Filter.pm -- be
> provide to vastly simplify the task of source code filtering,
> at least in common cases.

Can I suggest you feed data a line at a time rather than the series of lines
as you do right now. That makes it work like the existing filters.

I'm not sure Filter is the best name for this. How about Filter::Transform
or Filter::Simple?

Paul


__________________________________________________
Do You Yahoo!?
Talk to your friends online with Yahoo! Messenger.
http://im.yahoo.com

Reply via email to