.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.40) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Class::Autouse 3pm" .TH Class::Autouse 3pm "2021-01-03" "perl v5.32.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Class::Autouse \- Run\-time load a class the first time you call a method in it. .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 2 \& ################################################################## \& # SAFE FEATURES \& \& # Debugging (if you go that way) must be set before the first use \& BEGIN { \& $Class::Autouse::DEBUG = 1; \& } \& \& # Turn on developer mode (always load immediately) \& use Class::Autouse qw{:devel}; \& \& # Load a class on method call \& use Class::Autouse; \& Class::Autouse\->autouse( \*(AqCGI\*(Aq ); \& print CGI\->b(\*(AqWow!\*(Aq); \& \& # Use as a pragma \& use Class::Autouse qw{CGI}; \& \& # Use a whole module tree \& Class::Autouse\->autouse_recursive(\*(AqAcme\*(Aq); \& \& # Disable module\-existance check, and thus one additional \*(Aqstat\*(Aq \& # per module, at autouse\-time if loading modules off a remote \& # network drive such as NFS or SMB. \& # (See below for other performance optimizations.) \& use Class::Autouse qw{:nostat}; \& \& \& \& \& \& ################################################################## \& # UNSAFE FEATURES \& \& # Turn on the Super Loader (load all classes on demand) \& use Class::Autouse qw{:superloader}; \& \& # Autouse classes matching a given regular expression \& use Class::Autouse qr/::Test$/; \& \& # Install a class generator (instead of overriding UNIVERSAL::AUTOLOAD) \& # (See below for a detailed example) \& use Class::Autouse \e&my_class_generator; \& \& # Add a manual callback to UNIVERSAL::AUTOLOAD for syntactic sugar \& Class::Autouse\->sugar(\e&my_magic); .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" \&\fBClass::Autouse\fR is a runtime class loader that allows you to specify classes that will only load when a method of that class is called. .PP For large classes or class trees that might not be used during the running of a program, such as Date::Manip, this can save you large amounts of memory, and decrease the script load time a great deal. .PP \&\fBClass::Autouse\fR also provides a number of \*(L"unsafe\*(R" features for runtime generation of classes and implementation of syntactic sugar. These features make use of (evil) \s-1UNIVERSAL::AUTOLOAD\s0 hooking, and are implemented in this class because these hooks can only be done by a one module, and Class::Autouse serves as a useful place to centralise this kind of evil :) .SS "Class, not Module" .IX Subsection "Class, not Module" The terminology \*(L"class loading\*(R" instead of \*(L"module loading\*(R" is used intentionally. Modules will only be loaded if they are acting as a class. .PP That is, they will only be loaded during a Class\->method call. If you try to use a subroutine directly, say with \f(CW\*(C`Class::method()\*(C'\fR, the class will not be loaded and a fatal error will mostly likely occur. .PP This limitation is made to allow more powerful features in other areas, because we can focus on just loading the modules, and not have to deal with importing. .PP And really, if you are doing \s-1OO\s0 Perl, you should be avoiding importing wherever possible. .SS "Use as a pragma" .IX Subsection "Use as a pragma" Class::Autouse can be used as a pragma, specifying a list of classes to load as the arguments. For example .PP .Vb 1 \& use Class::Autouse qw{CGI Data::Manip This::That}; .Ve .PP is equivalent to .PP .Vb 4 \& use Class::Autouse; \& Class::Autouse\->autouse( \*(AqCGI\*(Aq ); \& Class::Autouse\->autouse( \*(AqData::Manip\*(Aq ); \& Class::Autouse\->autouse( \*(AqThis::That\*(Aq ); .Ve .SS "Developer Mode" .IX Subsection "Developer Mode" \&\f(CW\*(C`Class::Autouse\*(C'\fR features a developer mode. In developer mode, classes are loaded immediately, just like they would be with a normal 'use' statement (although the import sub isn't called). .PP This allows error checking to be done while developing, at the expense of a larger memory overhead. Developer mode is turned on either with the \&\f(CW\*(C`devel\*(C'\fR method, or using :devel in any of the pragma arguments. For example, this would load \s-1CGI\s0.pm immediately .PP .Vb 1 \& use Class::Autouse qw{:devel CGI}; .Ve .PP While developer mode is roughly equivalent to just using a normal use command, for a large number of modules it lets you use autoloading notation, and just comment or uncomment a single line to turn developer mode on or off. You can leave it on during development, and turn it off for speed reasons when deploying. .SS "Recursive Loading" .IX Subsection "Recursive Loading" As an alternative to the super loader, the \f(CW\*(C`autouse_recursive\*(C'\fR and \&\f(CW\*(C`load_recursive\*(C'\fR methods can be used to autouse or load an entire tree of classes. .PP For example, the following would give you access to all the \s-1URI\s0 related classes installed on the machine. .PP .Vb 1 \& Class::Autouse\->autouse_recursive( \*(AqURI\*(Aq ); .Ve .PP Please note that the loadings will only occur down a single branch of the include path, whichever the top class is located in. .SS "No-Stat Mode" .IX Subsection "No-Stat Mode" For situations where a module exists on a remote disk or another relatively expensive location, you can call \f(CW\*(C`Class::Autouse\*(C'\fR with the :nostat param to disable initial file existance checking at hook time. .PP .Vb 2 \& # Disable autoload\-time file existance checking \& use Class::Autouse qw{:nostat}; .Ve .SS "Super Loader Mode" .IX Subsection "Super Loader Mode" Turning on the \f(CW\*(C`Class::Autouse\*(C'\fR super loader allows you to automatically load \fB\s-1ANY\s0\fR class without specifying it first. Thus, the following will work and is completely legal. .PP .Vb 1 \& use Class::Autouse qw{:superloader}; \& \& print CGI\->b(\*(AqWow!\*(Aq); .Ve .PP The super loader can be turned on with either the \&\f(CW\*(C`Class::Autouse\->\*(C'\fRsuperloader> method, or the \f(CW\*(C`:superloader\*(C'\fR pragma argument. .PP Please note that unlike the normal one-at-a-time autoloading, the super-loader makes global changes, and so is not completely self-contained. .PP It has the potential to cause unintended effects at a distance. If you encounter unusual behaviour, revert to autousing one-at-a-time, or use the recursive loading. .PP Use of the Super Loader is highly discouraged for widely distributed public applications or modules unless unavoidable. \fBDo not use\fR just to be lazy and save a few lines of code. .SS "Loading with Regular Expressions" .IX Subsection "Loading with Regular Expressions" As another alternative to the superloader and recursive loading, a compiled regular expression (qr//) can be supplied as a loader. Note that this loader implements \s-1UNIVERSAL::AUTOLOAD,\s0 and has the same side effects as the superloader. .SS "Registering a Callback for Dynamic Class Creation" .IX Subsection "Registering a Callback for Dynamic Class Creation" If none of the above are sufficient, a \s-1CODE\s0 reference can be given to Class::Autouse. Any attempt to call a method on a missing class will launch each registered callback until one returns true. .PP Since overriding \s-1UNIVERSAL::AUTOLOAD\s0 can be done only once in a given Perl application, this feature allows \s-1UNIVERSAL::AUTOLOAD\s0 to be shared. Please use this instead of implementing your own \s-1UNIVERSAL::AUTOLOAD.\s0 .PP See the warnings under the \*(L"Super Loader Module\*(R" above which apply to all of the features which override \s-1UNIVERSAL::AUTOLOAD.\s0 .PP It is up to the callback to define the class, the details of which are beyond the scope of this document. See the example below for a quick reference: .PP \fICallback Example\fR .IX Subsection "Callback Example" .PP Any use of a class like Foo::Wrapper autogenerates that class as a proxy around Foo. .PP .Vb 10 \& use Class::Autouse sub { \& my ($class) = @_; \& if ($class =~ /(^.*)::Wrapper/) { \& my $wrapped_class = $1; \& eval "package $class; use Class::AutoloadCAN;"; \& die $@ if $@; \& no strict \*(Aqrefs\*(Aq; \& *{$class . \*(Aq::new\*(Aq } = sub { \& my $class = shift; \& my $proxy = $wrapped_class\->new(@_); \& my $self = bless({proxy => $proxy},$class); \& return $self; \& }; \& *{$class . \*(Aq::CAN\*(Aq } = sub { \& my ($obj,$method) = @_; \& my $delegate = $wrapped_class\->can($method); \& return unless $delegate; \& my $delegator = sub { \& my $self = shift; \& if (ref($self)) { \& return $self\->{proxy}\->$method(@_); \& } \& else { \& return $wrapped_class\->$method(@_); \& } \& }; \& return *{ $class . \*(Aq::\*(Aq . $method } = $delegator; \& }; \& \& return 1; \& } \& return; \& }; \& \& package Foo; \& sub new { my $class = shift; bless({@_},$class); } \& sub class_method { 123 } \& sub instance_method { \& my ($self,$v) = @_; \& return $v * $self\->some_property \& } \& sub some_property { shift\->{some_property} } \& \& \& package main; \& my $x = Foo::Wrapper\->new( \& some_property => 111, \& ); \& print $x\->some_property,"\en"; \& print $x\->instance_method(5),"\en"; \& print Foo::Wrapper\->class_method,"\en"; .Ve .SS "sugar" .IX Subsection "sugar" This method is provided to support \*(L"syntactic sugar\*(R": allowing the developer to put things into Perl which do not look like regular Perl. There are several ways to do this in Perl. Strategies which require overriding \&\s-1UNIVERSAL::AUTOLOAD\s0 can use this interface instead to share that method with the superloader, and with class gnerators. .PP When Perl is unable to find a subroutine/method, and all of the class loaders are exhausted, callbacks registered via \fBsugar()\fR are called. The callbacks receive the class name, method name, and parameters of the call. .PP If the callback returns nothing, Class::Autouse will continue to iterate through other callbacks. The first callback which returns a true value will end iteration. That value is expected to be a \s-1CODE\s0 reference which will respond to the \s-1AUTOLOAD\s0 call. .PP Note: The sugar callback(s) will only be fired by \s-1UNIVERSAL::AUTOLOAD\s0 after all other attempts at loading the class are done, and after attempts to use regular \&\s-1AUTOLOAD\s0 to handle the method call. It is never fired by \fBisa()\fR or \fBcan()\fR. It will fire repatedly for the same class. To generate classes, use the regular \s-1CODE\s0 ref support in \fBautouse()\fR. .PP \fISyntactic Sugar Example\fR .IX Subsection "Syntactic Sugar Example" .PP .Vb 11 \& use Class::Autouse; \& Class::Autouse\->sugar( \& sub { \& my $caller = caller(1); \& my ($class,$method,@params) = @_; \& shift @params; \& my @words = ($method,$class,@params); \& my $sentence = join(" ",@words); \& return sub { $sentence }; \& } \& ); \& \& $x = trolls have big ugly hairy feet; \& \& print $x,"\en"; \& # trolls have big ugly hairy feet .Ve .SS "mod_perl" .IX Subsection "mod_perl" The mechanism that \f(CW\*(C`Class::Autouse\*(C'\fR uses is not compatible with mod_perl. In particular with reloader modules like Apache::Reload. \f(CW\*(C`Class::Autouse\*(C'\fR detects the presence of mod_perl and acts as normal, but will always load all classes immediately, equivalent to having developer mode enabled. .PP This is actually beneficial, as under mod_perl classes should be preloaded in the parent mod_perl process anyway, to prevent them having to be loaded by the Apache child classes. It also saves \s-1HUGE\s0 amounts of memory. .PP Note that dynamically generated classes and classes loaded via regex \s-1CANNOT\s0 be pre-loaded automatically before forking child processes. They will still be loaded on demand, often in the child process. See prefork below. .SS "prefork" .IX Subsection "prefork" As with mod_perl, \f(CW\*(C`Class::Autouse\*(C'\fR is compatible with the prefork module, and all modules specifically autoloaded will be loaded before forking correctly, when requested by prefork. .PP Since modules generated via callback or regex cannot be loaded automatically by prefork in a generic way, it's advised to use prefork directly to load/generate classes when using mod_perl. .SS "Performance Optimizatons" .IX Subsection "Performance Optimizatons" .IP ":nostat" 4 .IX Item ":nostat" Described above, this option is useful when the module in question is on remote disk. .IP ":noprebless" 4 .IX Item ":noprebless" When set, Class::Autouse presumes that objects which are already blessed have their class loaded. .Sp This is true in most cases, but will break if the developer intends to reconstitute serialized objects from Data::Dumper, FreezeThaw or its cousins, and has configured Class::Autouse to load the involved classes just-in-time. .IP ":staticisa" 4 .IX Item ":staticisa" When set, presumes that \f(CW@ISA\fR will not change for a class once it is loaded. The greatest grandparent of a class will be given back the original can/isa implementations which are faster than those Class::Autouse installs into \&\s-1UNIVERSAL.\s0 This is a performance tweak useful in most cases, but is left off by default to prevent obscure bugs. .SS "The Internal Debugger" .IX Subsection "The Internal Debugger" Class::Autouse provides an internal debugger, which can be used to debug any weird edge cases you might encounter when using it. .PP If the \f(CW$Class::Autouse::DEBUG\fR variable is true when \f(CW\*(C`Class::Autouse\*(C'\fR is first loaded, debugging will be compiled in. This debugging prints output like the following to \s-1STDOUT.\s0 .PP .Vb 9 \& Class::Autouse::autouse_recursive( \*(AqFoo\*(Aq ) \& Class::Autouse::_recursive( \*(AqFoo\*(Aq, \*(Aqload\*(Aq ) \& Class::Autouse::load( \*(AqFoo\*(Aq ) \& Class::Autouse::_children( \*(AqFoo\*(Aq ) \& Class::Autouse::load( \*(AqFoo::Bar\*(Aq ) \& Class::Autouse::_file_exists( \*(AqFoo/Bar.pm\*(Aq ) \& Class::Autouse::load \-> Loading in Foo/Bar.pm \& Class::Autouse::load( \*(AqFoo::More\*(Aq ) \& etc... .Ve .PP Please note that because this is optimised out if not used, you can no longer (since 1.20) enable debugging at run-time. This decision was made to remove a large number of unneeded branching and speed up loading. .SH "METHODS" .IX Header "METHODS" .ie n .SS "autouse $class, ..." .el .SS "autouse \f(CW$class\fP, ..." .IX Subsection "autouse $class, ..." The autouse method sets one or more classes to be loaded as required. .ie n .SS "load $class" .el .SS "load \f(CW$class\fP" .IX Subsection "load $class" The load method loads one or more classes into memory. This is functionally equivalent to using require to load the class list in, except that load will detect and remove the autoloading hook from a previously autoused class, whereas as use effectively ignore the class, and not load it. .SS "devel" .IX Subsection "devel" The devel method sets development mode on (argument of 1) or off (argument of 0). .PP If any classes have previously been autouse'd and not loaded when this method is called, they will be loaded immediately. .SS "superloader" .IX Subsection "superloader" The superloader method turns on the super loader. .PP Please note that once you have turned the superloader on, it cannot be turned off. This is due to code that might be relying on it being there not being able to autoload its classes when another piece of code decides they don't want it any more, and turns the superloader off. .ie n .SS "class_exists $class" .el .SS "class_exists \f(CW$class\fP" .IX Subsection "class_exists $class" Handy method when doing the sort of jobs that \f(CW\*(C`Class::Autouse\*(C'\fR does. Given a class name, it will return true if the class can be loaded ( i.e. in \f(CW@INC\fR ), false if the class can't be loaded, and undef if the class name is invalid. .PP Note that this does not actually load the class, just tests to see if it can be loaded. Loading can still fail. For a more comprehensive set of methods of this nature, see Class::Inspector. .ie n .SS "autouse_recursive $class" .el .SS "autouse_recursive \f(CW$class\fP" .IX Subsection "autouse_recursive $class" The same as the \f(CW\*(C`autouse\*(C'\fR method, but autouses recursively. .ie n .SS "load_recursive $class" .el .SS "load_recursive \f(CW$class\fP" .IX Subsection "load_recursive $class" The same as the \f(CW\*(C`load\*(C'\fR method, but loads recursively. Great for checking that a large class tree that might not always be loaded will load correctly. .SH "SUPPORT" .IX Header "SUPPORT" Bugs should be always be reported via the \s-1CPAN\s0 bug tracker at .PP .PP For other issues, or commercial enhancement or support, contact the author. .SH "AUTHORS" .IX Header "AUTHORS" Adam Kennedy .PP Scott Smith .PP Rob Napier .SH "SEE ALSO" .IX Header "SEE ALSO" autoload, autoclass .SH "COPYRIGHT" .IX Header "COPYRIGHT" Copyright 2002 \- 2012 Adam Kennedy. .PP This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. .PP The full text of the license can be found in the \&\s-1LICENSE\s0 file included with this module.