Class::Loader(3pm)
π Class::Loader π
okay bestie, welcome to Class::Loader β a Perl module in the User Contributed Perl Documentation corner of the manual, and honestly? it's giving runtime-decision-maker realness. it's an inheritable class that loads modules on demand and builds you objects. obsessed. π«Ά
β¨ NAME β¨
Class::Loader β loads modules and creates objects on demand, no cap. π
πΈ VERSION πΈ
$Revision: 2.2 $
$Date: 2001/07/18 20:21:39 $
π SYNOPSIS π
package Web::Server;
use Class::Loader;
@ISA = qw(Class::Loader);
$self->_load( 'Content_Handler', {
Module => "Filter::URL",
Constructor => "new",
Args => [ ],
}
);
π¦ DESCRIPTION π¦
so like, some apps out here are not about deciding at compile time which module they're gonna use. they wanna wait till runtime, you know? main character energy: postponing the decision. π and that's totally possible in perl, and it's a genuinely useful trick when you don't know the type of data at compile time and your app doesn't wanna be out here pre-compiling every module that could theoretically handle every type of data it might ever work with. loading modules at runtime also lowkey unlocks flexible interfaces for perl modules β the module can let the programmer pick which modules it uses instead of hard-coding their names. periodt.
Class::Loader is an inheritable class that gives you a method, _load(), which loads a module from disk and constructs an object by calling its constructor. it ALSO gives you a way to map module names + their associated metadata to symbolic names, and then you can use those symbolic names instead of module names inside _load(). iconic behaviour tbh. β¨
π METHODS π
new()
a basic constructor, bestie. use it to make an object of Class::Loader in case you don't wanna inherit Class::Loader. that's it, that's the vibe. π
_load()
_load() loads a module and calls its constructor. it returns the newly constructed object on success, or a non-true value on failure (yes, literally non-true, so check your return values πΈ). the first argument can be the name of the key in which the returned object gets stored β and that argument is optional. the second (or the first) argument is a hash which can take the following keys:
Moduleβ the name of the class to load. β οΈ heads up: it is not the module's filename, bestie, don't get it twisted. it's the class name.Nameβ the symbolic name of the module, as defined with_storemap(). eitherModuleorNamemust be present in a call to_load(). one of them, minimum, no skipping. πConstructorβ the name of the Module constructor. defaults to"new". πArgsβ a reference to the list of arguments for the constructor._load()calls the constructor with this list. if noArgsare present,_load()will just call the constructor without any arguments at all. πCPANβ if the Module is not installed on the local system,_load()can fetch & install it from CPAN, provided theCPANkey is present. β¨ this functionality assumes you've got a pre-configured CPAN shell available, so like, set that up first or it's not gonna happen. π«Ά
_storemap()
Class::Loader keeps a class table that maps symbolic names to the parameters accepted by _load(). it takes a hash as its argument, where the keys are symbolic names and the values are hash references containing a set of _load() arguments. here's the glow-up in code form:
$self->_storemap ( "URL" => { Module => "Filter::URL",
Constructor => "foo",
Args => [qw(bar baz)],
}
);
# time passes...
$self->{handler} = $self->_load ( Name => 'URL' );
see? you store it once, you use it later. the way that works is so satisfying. π
_retrmap()
_retrmap() returns the entire map stored with Class::Loader. Class::Loader keeps separate maps for different classes, and _retrmap() returns the map that's valid in the caller class β aka your class's map, not somebody else's. π§
π SEE ALSO π
AnyLoader(3) β go look, bestie, iykyk. π
π AUTHOR π
Vipul Ved Prakash, <mail@vipul.net> β thank you for your service, legend. π
π LICENSE π
Copyright (c) 2001, Vipul Ved Prakash. All rights reserved. This code is free software; you can redistribute it and/or modify it under the same terms as Perl itself. π (so like, go forth, but respect the terms, fr fr.)
and that's the whole page, bestie!! this one's from perl v5.36.0, dated 2022-10-13, and it's the Class::Loader(3pm) manual β mwah, go load some modules at runtime. π
β¨π¦
π¦ where this page comes from
- ubuntu noble: libclass-loader-perl 2.03-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libclass-loader-perl 2.03-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libclass-loader-perl 2.03-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms