What exactly is a source filter?
Asked Answered
K

3

15

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.

Kahn answered 9/8, 2011 at 3:54 Comment(1)
@mu is too short : To be quite honest, I don't know if this is something Perl-specific or notKahn
S
19

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.

Subcontract answered 9/8, 2011 at 4:19 Comment(3)
I didn't know it was there in perldoc!Kahn
It is (provided you know your way around)Kahn
That is hard, of course you can search the faq by invoking perldoc -q <searchterm>, unfortunately this doesn't help for filter. I guess you can do perldoc perltoc and search /filter which gets you there eventually. Of course perldoc.perl.org and p3rl.org are helpful too.Subcontract
R
7

A source filter is a form of module which affects the way in which a file use-ing it will be parsed. They are commonly used to simulate syntactical features which Perl does not have natively -- for instance, the Switch source filter was often used to simulate switch statements before Perl's given { } construction was available.

Source filters work by taking the text of the module as input, performing some processing on it, and outputting the filtered source code. For a simple example of how a source filter is implemented, as well as more details, see the perldoc page for perlfilter.

Registered answered 9/8, 2011 at 4:18 Comment(0)
S
4

They are pre-processors. They change the source code before it reaches the Perl compiler. You can do scary things with them, in effect implementing your own language, with all the effects this has on readability (for others), robustness (writing parsers is hard) and maintainability (debugging gets tricky when your idea of what the source code is differs from what compiler and runtime think it is).

Supranatural answered 9/8, 2011 at 4:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.