Softpedia
 


LINUX CATEGORIES:



GLOBAL PAGES >>
NEWS ARCHIVE >>
SOFTPEDIA REVIEWS >>
MEET THE EDITORS >>
WEEK'S BEST
  • Linux Kernel 3.9.3 / 3....
  • LibreOffice 3.6.6 / 4.0.3
  • MPlayer 1.1.1
  • systemd 204
  • Arch Linux 2013.05.01
  • Blender 2.67a
  • KDE Software Compilatio...
  • CrunchBang Linux Stable...
  • Elementary OS 0.1 / 0.2...
  • SystemRescueCd 3.6.0
  • Home > Linux > Programming > Perl Modules

    Filter::Simple 0.82

    Download button

    No screenshots available
    Downloads: 397  View global page NEW!  Tell us about an update
    User Rating:
    Rated by:
    Fair (2.7/5)
    12 user(s)
    Developer:

    License / Price:

    Last Updated:

    Category:
    Damian Conway | More programs
    Perl Artistic License / FREE
    February 16th, 2008, 00:02 GMT
    ROOT / Programming / Perl Modules

     Read user reviews (0)  Refer to a friend  Subscribe

    Filter::Simple description

    Filter::Simple module allows simplified source filtering.

    Filter::Simple module allows simplified source filtering.

    SYNOPSIS

    # in MyFilter.pm:

    package MyFilter;

    use Filter::Simple;

    FILTER { ... };

    # or just:
    #
    # use Filter::Simple sub { ... };

    # in user's code:

    use MyFilter;

    # this code is filtered

    no MyFilter;

    # this code is not

    The Problem

    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 excellent Filter::Util::Call module (by Paul Marquess) provides a usable Perl interface to source filtering, but it is often too powerful and not nearly as simple as it could be.

    To use the module it is necessary to do the following:

    Download, build, and install the Filter::Util::Call module. (If you have Perl 5.7.1 or later, this is already done for you.)

    Set up a module that does a use Filter::Util::Call.

    Within that module, create an import subroutine.

    Within the import subroutine do a call to filter_add, passing it either a subroutine reference.

    Within the subroutine reference, call filter_read or filter_read_exact to "prime" $_ with source code data from the source file that will use your module. Check the status value returned to see if any source code was actually read in.

    Process the contents of $_ to change the source code in the desired manner.
    Return the status value.

    If the act of unimporting your module (via a no) should cause source code filtering to cease, create an unimport subroutine, and have it call filter_del. Make sure that the call to filter_read or filter_read_exact in step 5 will not accidentally read past the no. Effectively this limits source code filters to line-by-line operation, unless the import subroutine does some fancy pre-pre-parsing of the source code it's filtering.

    For example, here is a minimal source code filter in a module named BANG.pm. It simply converts every occurrence of the sequence BANGs+BANG to the sequence die 'BANG' if $BANG in any piece of code following a use BANG; statement (until the next 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*nos+$callers*;s*?$/) {
    $no_seen=1;
    last;
    }
    $data .= $_;
    $_ = "";
    }
    $_ = $data;
    s/BANGs+BANG/die 'BANG' if $BANG/g
    unless $status < 0;
    $_ .= "no $class;n" if $no_seen;
    return 1;
    })
    }

    sub unimport {
    filter_del();
    }

    1 ;
    This level of sophistication puts filtering out of the reach of many programmers.

    A Solution

    The Filter::Simple module provides a simplified interface to Filter::Util::Call; one that is sufficient for most common cases.

    Instead of the above process, with Filter::Simple the task of setting up a source code filter is reduced to:

    Download and install the Filter::Simple module. (If you have Perl 5.7.1 or later, this is already done for you.)

    Set up a module that does a use Filter::Simple and then calls FILTER { ... }.
    Within the anonymous subroutine or block that is passed to FILTER, process the contents of $_ to change the source code in the desired manner.
    In other words, the previous example, would become:

    package BANG;
    use Filter::Simple;

    FILTER {
    s/BANGs+BANG/die 'BANG' if $BANG/g;
    };

    1 ;

    Note that the source code is passed as a single string, so any regex that uses ^ or $ to detect line boundaries will need the /m flag.

    Requirements:

    · Perl



    Product's homepage

    Requirements:

    · Perl

      


    TAGS:

    source filtering | simplified filtering | Perl module | source | filtering | Perl

    Go to top

    WindowsGamesDriversMacLinuxScriptsMobileHandheldNews

    SUBMIT PROGRAM   |   ADVERTISE   |   GET HELP   |   SEND US FEEDBACK   |   RSS FEEDS   |   UPDATE YOUR SOFTWARE   |   ROMANIAN FORUM