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.
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.
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.
perldoc
! –
Kahn 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 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.
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).
© 2022 - 2024 — McMap. All rights reserved.