Class::Loader(3pm)

πŸ“¦
version 2.03 Β· 6 versions
, in ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– 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(). either Module or Name must 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 no Args are 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 the CPAN key 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