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
    I've read that when working on programs where memory usage is important, it is best to avoid importing symbols from other packages
    Importing stuff from modules isn't that expensive, and avoiding importing it won't save much. You see, the subs etc in the module, already exist in memory, as soon as the module is loaded. Even if you never call them, or even don't import them.

    All that import does, is add a new name for the same sub in the symbol table for the current package. There is no recompilation, not even a copy of the sub body, for the new import!

    n.b. The symbol table, or stash, is nothing but a plain hash containing globs, which are just binary structures with refs to various types of data sharing the same name — for example a reference to a sub body in the slot for the code ref.

    So... I think you must misremember what you read. It is recommended not to import stuff from modules that you don't use, but not to save on memory, but in order to not to clutter the symbol table with useless stuff. Because every imported name restricts the choice you have for your own names of subs — thus: to prevent name clashes.

Re: Idea for new module/pragma
by ysth (Canon) on Oct 04, 2006 at 09:18 UTC
    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.
    But not everything that exports uses Exporter, e.g. CGI.

    Someone that concerned could just avoid default importing by always specifying a list of the actually desired imports. This would take care of the really expensive stuff like use POSIX;. Avoiding all importing is not likely to save much memory.

    The phrase "Sufficiently Advanced Technology" made me think of "Enough Rope".

Re: Idea for new module/pragma
by CountZero (Bishop) on Oct 04, 2006 at 06:24 UTC
    Interesting. Did you test how much memory you saved?

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law