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.


In reply to Idea for new module/pragma by zealot

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.