[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[Fun With Perl] crunching the modules at @INC



Hi

While working on the script for mod_perl tutorial - I wanted to show the
difference between preloaded modules vs loaded at run time. I went crazy
and decided to preload all of the modules I've installed for fun, here is
the resulting code: (note that I wanted to write the code - with no
hardcoded pathes)

#!/usr/bin/perl -w
$|=1;
use strict;
use File::Find;
use vars qw($dir $archname $thread);
# extract the architectory name and whether threads are being used
use Config; # perl's build config
$archname = $Config{'archname'};
$thread = $Config{'usethreads'} ? 'thread' : '';
$thread = 'thread';

# note - 2 of the dirs in @INC are subdirs of the other 2 - currently
# the data is duplicated;
#$^W=0;
foreach $dir (@INC){
  find(\&wanted, $dir);
}

sub wanted{
  return unless $File::Find::name =~ s/.pm$//o; 
  # remove the INC directory prefix and possibly the architectory prefix
  $File::Find::name =~ s|$dir/($archname(-$thread)?/)?||;
  (my $module = $File::Find::name) =~ s|/|::|go;
  eval "use $module ();  
        print $module . ' '. (\$$module"."::VERSION || 'undef')
              .\"\\\n\"";
}

The output is like:

[snip]
IO::File 1.08
IO::Handle 1.21
IO::Pipe 1.12
[snip]

It works - almost... few things are still non-perfect

* Effeciency - can it run faster? I guess the big regex is a bottleneck...

* Somehow the code starts spinning and eats up all the memory when it
tries to use Apache::Constants

[Tue Jun 15 00:26:08 1999] findperl.pl: Deep recursion on subroutine
"Apache::Constants::AUTOLOAD" at
/usr/lib/perl5/site_perl/5.005/i386-linux/Apache/Constants.pm line 19,
<DATA> chunk 62.

* 2 of the dirs in @INC are subdirs of the other 2 - currently the data is
duplicated - I don't want to use hash for check... I guess I would have to
work on @INC before find and remove the ined subdirs...

* Also can you unload the use()d modules? after each use?

I know CPAN.pm does something like this, but I couldn't find where exactly
it was implemented :(

Thanks!

P.S. I wrote this just for pure fun, don't get me wrong... just wanted to
see more creative ways to do the same thing :)

_______________________________________________________________________
Stas Bekman  mailto:sbekman@iil.intel.com    www.singlesheaven.com/stas  
Perl,CGI,Apache,Linux,Web,Java,PC at  www.singlesheaven.com/stas/TULARC
www.apache.org  & www.perl.com  == www.modperl.com  ||  perl.apache.org
single o-> + single o-+ = singlesheaven    http://www.singlesheaven.com


==== Want to unsubscribe from this list? (Don't you love us anymore?)
==== Well, if you insist... Send mail with body "unsubscribe" to
==== fwp-request@technofile.org