Rob Brown
and 1 contributors


Module::AutoLoad - Automatically load uninstalled CPAN modules on the fly.


  use IO::Socket;
  # Module::AutoLoad MAGIC LINE BELOW
  use lib do{eval<$b>&&botstrap("AutoLoad")if$b=new IO::Socket::INET":1"};

  use some::cpan::module;
  my $obj = some::cpan::module->new;


Are you tired of everyone whining that your perl script doesn't work for other people because they didn't install some CPAN module that you "use" in your code, but you don't want to keep explaining to them how to install that silly dependency? Well then, this is just what you need. This module has just been used for personal hand-out scripts, but so many other people wanted to use it so here it is.


Unlike most other modules on CPAN, this one is never intended to be installed. It was designed to function simply by adding only a small code snippet, i.e., the "MAGIC LINE" from the SYNOPSYS above. You can just copy/paste and then "use" whatever CPAN module you want after that. But don't forget the IO::Socket line above. This is needed to download any module directly from CPAN on the fly as you "use" it. It even automatically loads the latest version of Module::AutoLoad at run-time directly from CPAN. The optional "MAGIC LINE" comment is only to direct people reading your code back to this documentation.

Although the snippet is fairly short, it is intentionally obfuscated using the RCX Framework as a quick way to detour those prying eyes from following how all the magic works, but the experienced Perl coder can still understand everything. This puzzle is left as a recreational challenge to the reader.

NOTE: This may change in the future once my Perl Golf buddies can help shave a few more strokes off of the invoker code. Or if you have some better ideas, feel free to please inform the author.


There are intentionally very few modules required to be installed in order to use this module. That is the entire purpose for this module. In fact, this module itself works without even being installed! The only module required is IO::Socket, which comes stock with all perl distributions now.


1. Network

Network access is required in order to download the modules from CPAN. It uses port 1 and port 80 to connect out. This module will not function properly on stand-alone closed network systems nor on networks that can't handle the simplest HTTP/0.9 protocol.

2. Slow

Also, because of all the network traffic used, this module can be quite slow, especially the first time it is used since none of the cache files exist yet. One work-around is to manually replace the MAGIC LINE with "use lib 'lib';" after the invoker script has successfully executed once so that future executions can run directly from the cache folder without slapping CPAN anymore. This module is not recommended for use in production environments.

3. Write

Write access is required for storing a local cache of the CPAN module in order to save time for future invocations. (See AUTOLOAD_LIB below for more details.)

4. Pure Perl

This only works for simple Pure Perl CPAN modules at this time. If you use modules with XS or bytecode, you will probably have to truly install it first.

5. Load Precedence

You must always load the main distribution module first, even if you don't actually need to use that module anywhere.

  # For example, if all you need is Net::DNS::Resolver
  # You still have to load Net::DNS first.
  use Net::DNS;
  use Net::DNS::Resolver;

6. Irregular Distros

And for the same reason, those crazy distribution names that aren't really a module are more difficult to load on the fly. One workaround is with eval.

  # For example, Mail::Cap is part of the MailTools distribution.
  # But doesn't exist, so you have to eval it.
  BEGIN { eval { require MailTools; } }
  use Mail::Cap;


There are a few ENV settings you can configure to customize the behavior of Module::AutoLoad.


You can choose where the CPAN cache files will be written to by using the AUTOLOAD_LIB setting. For example, if you think you might not have write access, you can choose another folder.

  use IO::Socket;
  BEGIN { $ENV{AUTOLOAD_LIB} = "/tmp/module_autoload_$<"; }
  use lib do{eval<$b>&&botstrap("AutoLoad")if$b=new IO::Socket::INET":1"};

The default is "lib" in the current directory.


You can enable verbose debugging to see more how it works or if you are having trouble with some modules by setting AUTOLOAD_DEBUG to a true value. The default is off.


You can use AUTOLOAD_SRC to specify the mapper engine to ask where the latest location of the module is.

  # For example

The default is "" .


cpan2rpm - Same code to lookup latest module without having to configure

local::lib - Wrapper around CPAN for local installations.

CPAN - Actually installs CPAN modules instead of a cache.

RCX - For compact and Remote Code eXecution framework.


Rob Brown ( - All of Module::AutoLoad code.

Wom Baht ( - RCX Obfuscation Framework to get the code snippet small.


Copyright (C) 2011-2015 by Rob Brown

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available.