zealot has asked for the wisdom of the Perl Monks concerning the following question:
I recently attended a talk about Sufficiently Advanced Technology by Damian Conway. One of the items he touched upon was source filters, and that got me thinking of a possible new module.
I've read that when working on programs where memory usage is important, it is best to avoid importing symbols from other packages. Now it is not difficult to load a module importing an empty list, and then reference the fully qualified symbols instead.
The basic idea is to take code like this:
use absolutify; use English qw( -no_match_vars ); use Readonly qw( Readonly ); Readonly my $FOO => 12345; print "foo: $FOO\nprogram: $PROGRAM_NAME\n";
And then with a source code filter, update the code so what is actually compiled is:
use absolutify; use English (); use Readonly (); Readonly::Readonly my $FOO => 12345; print "foo: $FOO\nprogram: $English::PROGRAM_NAME\n";
I wrote a rough version to see if there was a chance that this code would work, and I have gotten something that correctly changes simple imports.
I am aware that some functions do more advanced and subtle things with the import function. My experience is that many would function correctly with this change.
Does this have a chance of being useful and helpful? Is it likely to cause problems that will make it too difficult to work correctly? I'm open to all comments.
The code that I wrote which is still very rough is:
package absolutify; use Filter::Simple; { my $USE_REGEX = qr{ ^ \s* ( use \s+ (\S+) \s+ ) qw[(] ([^\)]*) [)] ; }xms; my %Filter_Replace; FILTER { while ( s/ $USE_REGEX /$1();/xms ) { my ( $use_line, $package, $import ) = ( $1, $2, $3 ); my @imports = split /\s+/, $import; for my $import (@imports) { if ( $import =~ / \A ([&\$%@])? (\w+) \z /xms ) { $Filter_Replace{$import} = "$1$package\::$2"; } } } s/ ( [&\$%@]? \w+ ) / $Filter_Replace{$1} || $1 /xmsge; }; } 1;
Ideally, I'd like to be able to have the filter load the module directly. Then examine the Export variables if available for the module to know understand what would be exported by default and what tags would be allowed and such.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Idea for new module/pragma
by bart (Canon) on Oct 04, 2006 at 10:37 UTC | |
|
Re: Idea for new module/pragma
by ysth (Canon) on Oct 04, 2006 at 09:18 UTC | |
|
Re: Idea for new module/pragma
by CountZero (Bishop) on Oct 04, 2006 at 06:24 UTC |