Source Filters
If you can write a program to translate random stuff into Perl, then why not have a way of invoking that translator from within Perl?
The notion of a source filter started with the idea that a script or module should be able to decrypt itself on the fly, like this:
#!/usr/bin/perl
use MyDecryptFilter;
@*x$]`0uN&k^Zx02jZ^X{.?s!(f;9Q/^A^@~~8H]|,%@^P:q–=
...But the idea grew from there, and now a source filter can be defined to do any transformation on the input text you like. Put that together with the notion of the –x switch mentioned in Chapter 17, and you have a general mechanism for pulling any chunk of program out of a message and executing it, regardless of whether it’s written in Perl.
Using the Filter module from CPAN, one can now even do things like
programming Perl in awk:
#!/usr/bin/perl
use Filter::exec "a2p"; # the awk–to–perl translator
1,30 { print $1 }Now that’s definitely what you might call idiomatic. But we won’t pretend for a moment that it’s common practice.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access