Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

ethan (3163)

  reversethis-{ed. ... rap.nov.olissat}

Being a 25-year old chap living in the western-most town of Germany. Stuying communication and information science and being a huge fan of XS-related things.

Journal of ethan (3163)

Friday April 04, 2003
02:45 AM

source filtering

[ #11440 ]

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), $_;


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.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • <plug type="CPAN module"> Module::MagicUse [] </plug>

    broquaint out

    • 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.