.\" Automatically generated by Pod::Man 4.09 (Pod::Simple 3.35) .\" .\" 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 .. .if !\nF .nr F 0 .if \nF>0 \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} .\} .\" ======================================================================== .\" .IX Title "Class::Load 3pm" .TH Class::Load 3pm "2018-07-21" "perl v5.26.2" "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::Load \- A working (require "Class::Name") and more .SH "VERSION" .IX Header "VERSION" version 0.25 .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use Class::Load \*(Aq:all\*(Aq; \& \& try_load_class(\*(AqClass::Name\*(Aq) \& or plan skip_all => "Class::Name required to run these tests"; \& \& load_class(\*(AqClass::Name\*(Aq); \& \& is_class_loaded(\*(AqClass::Name\*(Aq); \& \& my $baseclass = load_optional_class(\*(AqClass::Name::MightExist\*(Aq) \& ? \*(AqClass::Name::MightExist\*(Aq \& : \*(AqClass::Name::Default\*(Aq; .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" \&\f(CW\*(C`require EXPR\*(C'\fR only accepts \f(CW\*(C`Class/Name.pm\*(C'\fR style module names, not \&\f(CW\*(C`Class::Name\*(C'\fR. How frustrating! For that, we provide \&\f(CW\*(C`load_class \*(AqClass::Name\*(Aq\*(C'\fR. .PP It's often useful to test whether a module can be loaded, instead of throwing an error when it's not available. For that, we provide \&\f(CW\*(C`try_load_class \*(AqClass::Name\*(Aq\*(C'\fR. .PP Finally, sometimes we need to know whether a particular class has been loaded. Asking \f(CW%INC\fR is an option, but that will miss inner packages and any class for which the filename does not correspond to the package name. For that, we provide \f(CW\*(C`is_class_loaded \*(AqClass::Name\*(Aq\*(C'\fR. .SH "FUNCTIONS" .IX Header "FUNCTIONS" .SS "load_class Class::Name, \e%options" .IX Subsection "load_class Class::Name, %options" \&\f(CW\*(C`load_class\*(C'\fR will load \f(CW\*(C`Class::Name\*(C'\fR or throw an error, much like \f(CW\*(C`require\*(C'\fR. .PP If \f(CW\*(C`Class::Name\*(C'\fR is already loaded (checked with \f(CW\*(C`is_class_loaded\*(C'\fR) then it will not try to load the class. This is useful when you have inner packages which \f(CW\*(C`require\*(C'\fR does not check. .PP The \f(CW%options\fR hash currently accepts one key, \f(CW\*(C`\-version\*(C'\fR. If you specify a version, then this subroutine will call \f(CW\*(C`Class::Name\->VERSION( $options{\-version} )\*(C'\fR internally, which will throw an error if the class's version is not equal to or greater than the version you requested. .PP This method will return the name of the class on success. .SS "try_load_class Class::Name, \e%options \-> (0|1, error message)" .IX Subsection "try_load_class Class::Name, %options -> (0|1, error message)" Returns 1 if the class was loaded, 0 if it was not. If the class was not loaded, the error will be returned as a second return value in list context. .PP Again, if \f(CW\*(C`Class::Name\*(C'\fR is already loaded (checked with \f(CW\*(C`is_class_loaded\*(C'\fR) then it will not try to load the class. This is useful when you have inner packages which \f(CW\*(C`require\*(C'\fR does not check. .PP Like \f(CW\*(C`load_class\*(C'\fR, you can pass a \f(CW\*(C`\-version\*(C'\fR in \f(CW%options\fR. If the version is not sufficient, then this subroutine will return false. .SS "is_class_loaded Class::Name, \e%options \-> 0|1" .IX Subsection "is_class_loaded Class::Name, %options -> 0|1" This uses a number of heuristics to determine if the class \f(CW\*(C`Class::Name\*(C'\fR is loaded. There heuristics were taken from Class::MOP's old pure-perl implementation. .PP Like \f(CW\*(C`load_class\*(C'\fR, you can pass a \f(CW\*(C`\-version\*(C'\fR in \f(CW%options\fR. If the version is not sufficient, then this subroutine will return false. .SS "load_first_existing_class Class::Name, \e%options, ..." .IX Subsection "load_first_existing_class Class::Name, %options, ..." This attempts to load the first loadable class in the list of classes given. Each class name can be followed by an options hash reference. .PP If any one of the classes loads and passes the optional version check, that class name will be returned. If \fInone\fR of the classes can be loaded (or none pass their version check), then an error will be thrown. .PP If, when attempting to load a class, it fails to load because of a syntax error, then an error will be thrown immediately. .SS "load_optional_class Class::Name, \e%options \-> 0|1" .IX Subsection "load_optional_class Class::Name, %options -> 0|1" \&\f(CW\*(C`load_optional_class\*(C'\fR is a lot like \f(CW\*(C`try_load_class\*(C'\fR, but also a lot like \&\f(CW\*(C`load_class\*(C'\fR. .PP If the class exists, and it works, then it will return 1. If you specify a version in \f(CW%options\fR, then the version check must succeed or it will return 0. .PP If the class doesn't exist, and it appears to not exist on disk either, it will return 0. .PP If the class exists on disk, but loading from disk results in an error (e.g.: a syntax error), then it will \f(CW\*(C`croak\*(C'\fR with that error. .PP This is useful for using if you want a fallback module system, i.e.: .PP .Vb 1 \& my $class = load_optional_class($foo) ? $foo : $default; .Ve .PP That way, if \f(CW$foo\fR does exist, but can't be loaded due to error, you won't get the behaviour of it simply not existing. .SH "CAVEATS" .IX Header "CAVEATS" Because of some of the heuristics that this module uses to infer whether a module has been loaded, some false positives may occur in \f(CW\*(C`is_class_loaded\*(C'\fR checks (which are also performed internally in other interfaces) \*(-- if a class has started to be loaded but then dies, it may appear that it has already been loaded, which can cause other things to make the wrong decision. Module::Runtime doesn't have this issue, but it also doesn't do some things that this module does \*(-- for example gracefully handle packages that have been defined inline in the same file as another package. .SH "SEE ALSO" .IX Header "SEE ALSO" .IP "" 4 .IX Item "" This blog post is a good overview of the current state of the existing modules for loading other modules in various ways. .IP "" 4 .IX Item "" This blog post describes how to handle optional modules with Class::Load. .IP "" 4 .IX Item "" This Japanese blog post describes why DBIx::Skinny now uses Class::Load over its competitors. .IP "Moose, Jifty, Prophet, etc" 4 .IX Item "Moose, Jifty, Prophet, etc" This module was designed to be used anywhere you have \&\f(CW\*(C`if (eval "require $module"; 1)\*(C'\fR, which occurs in many large projects. .IP "Module::Runtime" 4 .IX Item "Module::Runtime" A leaner approach to loading modules .SH "SUPPORT" .IX Header "SUPPORT" Bugs may be submitted through the \s-1RT\s0 bug tracker (or bug\-Class\-Load@rt.cpan.org ). .PP There is also a mailing list available for users of this distribution, at . .PP There is also an irc channel available for users of this distribution, at \&\f(CW\*(C`#moose\*(C'\fR on \f(CW\*(C`irc.perl.org\*(C'\fR . .SH "AUTHOR" .IX Header "AUTHOR" Shawn M Moore .SH "CONTRIBUTORS" .IX Header "CONTRIBUTORS" .IP "\(bu" 4 Dave Rolsky .IP "\(bu" 4 Karen Etheridge .IP "\(bu" 4 Shawn Moore .IP "\(bu" 4 Jesse Luehrs .IP "\(bu" 4 Kent Fredric .IP "\(bu" 4 Paul Howarth .IP "\(bu" 4 Olivier Mengué .IP "\(bu" 4 Caleb Cushing .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" This software is copyright (c) 2008 by Shawn M Moore. .PP This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.