source filtering

ethan on 2003-04-04T07:45:19

Source filters are really cool and a nice playground. Here's one that eliminates the need for use() or require() most of the time:

package RealAutoUse; # autouse already exists but does something else

use strict; use Filter::Simple;

my %load;

use Filter::Simple;

FILTER_ONLY executable => sub { # perhaps a little too simple-minded, but alas while (/([A-Za-z0-9_:]+)\s*(?:->|::)(\s*\w+)/g) { $load{ $1 }++; } $_ = join "\n", map ("require $_;", keys %load), $_; }; 1;


Naturally, this is more useful with object-oriented modules:

use RealAutoUse;

my $cgi = CGI->new; print $cgi->header("text/plain"); print $cgi->param("para"); # but functional ones work, too

print POSIX::strftime("%H", localtime);


Too bad that mere filtering wont do for cases where modules export symbols etc., otherwise this little piece of code could have made it to the CPAN.


Module plug

broquaint on 2003-04-04T09:52:01

<plug type="CPAN module"> Module::MagicUse </plug>

Re:Module plug

ethan on 2003-04-04T12:02:06

Wasn't aware of it yet. You seem to be doing it a little differently by not letting one of the Filter modules change the code but instead string-evaling the necessary use-statements.

But yours can deal with indirect method invocation. I forgot about that.