perlsource-filter

What exactly is a source filter?


Whenever I see the term source filter I am left wondering as to what it refers to.

Aside from a formal definition, I think an example would also be helpful to drive the message home.


Solution

  • A source filter is a module that modifies some other code before it is evaluated. Therefore the code that is executed is not what the programmer sees when it is written. You can read more about source filters (in the Perl context) at perldoc perlfilter. Some examples are Smart::Comments which allows the programmer to leave debugging commands in comments in the code and employ them only if desired, another is PDL::NiceSlice which is sugar for slicing PDL objects.

    Edit:

    For more information on usage (should you wish to brave the beast), read the documentation for Filter::Simple which is a typical way to create filters.

    Alternatively there is a new and different way to muck about with the source: Devel::Declare lets you interact with Perl's own parser, letting you do many of the same type of thing as a source filter, but without the source filter. This can be "safer" in some respect, yet it has a more limited scope.