Class::Default(3pm)
π NAME π
Okay so bestie, welcome to Class::Default(3pm) β it's from the User Contributed Perl Documentation, and honestly? It's giving β¨magicβ¨. Here's the whole vibe in one line:
Class::Default β static calls apply to a default instantiation. periodt π
π¦ SYNOPSIS π¦
# Create the defaulted class
package Foo::Base;
use base 'Class::Default';
sub new { bless {}, $_[0] }
sub show {
my $self = shift->_self;
"$self";
}
# Do something to the default object
package main;
print Foo::Bar->show;
# Prints 'Foo::Bar=HASH(0x80d22f8)'
Look at that. You never made an object. You never called new. And yet. π«Ά
πΈ DESCRIPTION πΈ
Class::Default provides a mechanism to allow your class to take static method calls and apply it to a default instantiation of an object. It provides a flexibility to an API that allows it to be used more confortably in different situations. no cap, that's the whole point π
A good example of this technique in use is CGI.pm. When you use a static method, like CGI->header, your call is being applied to a default instantiation of a CGI object. iconic behaviour honestly.
This technique appears to be especially useful when writing modules that you want to be used in either a single use or a persistent environment. In a CGI like environment, you want the simplicity of a static interface. You can call Class->method directly, without having to pass an instantiation around constantly. literally the dream πβ¨
π USING THE MODULES π
Class::Default provides a couple of levels of control. They start with simple enabling the method to apply to the default instantation, and move on to providing some level of control over the creation of the default object. So likeβ¦ it holds your hand at first, then hands you the keys. main character energy fr fr π₯
π Inheriting from Class::Default π
To start, you will need to inherit from Class::Default. You do this in the normal manner, using something like use base 'Class::Default', or setting the @ISA value directly.
fun fact bestie: Class::Default does not have a default constructor or any public methods, so you should be able to use it a multiple inheritance situation without any implications. she's low maintenance, we love that. π
β¨ Making method work β¨
To make your class work with Class::Default you need to make a small adjustment to each method that you would like to be able to access the default object.
A typical method will look something like the following:
sub foobar {
my $self = shift;
# Do whatever the method does
}
To make the method work with Class::Default, you should change it to the following:
sub foobar {
my $self = shift->_self;
# Do whatever the method does
}
That's it. That's the whole glow-up. π¦ This change is very low impact, easy to use, and will not make any other differences to the way your code works. one line of code ate and left no crumbs.
π Control over the default object π
When needed, Class::Default will make a new instantation of your class and cache it to be used whenever a static call is made. It does this in the simplest way possible, by calling Class->new() with no arguments.
This is fine if you have a very pure class that can handle creating a new object without any arguments, but many classes expect some sort of argument to the the constructor, and indeed that the constructor that should be used it the new method. π not all of us are low demand okay!!
Enter the _create_default_object method. By overloading the _create_default_object method in your class, you can custom create the default object. This will used to create the default object on demand, the first time a method is called.
For example, the following class demonstrate the use of _create_default_object to set some values in the default object:
package Slashdot::User;
use base 'Class::Default';
# Constructor
sub new {
my $class = shift;
my $name = shift;
my $self = {
name => $name,
favourite_color => '',
};
return bless $self, $class;
}
# Default constructor
sub _create_default_object {
my $class = shift;
my $self = $class->new( 'Anonymous Coward' );
$self->{favourite_color} = 'Orange';
return $self;
}
sub name {
$_[0]->_self->{name};
}
sub favourite_color {
$_[0]->_self->{favourite_color};
}
That provides a statically accessible default object that could be used as in the following manner:
print "The default slashdot user is " . Slashdot::User->name
. " and they like the colour " . Slashdot::User->favourite_color;
Remember remember remember π the default object is persistent, so changes made to the statically accessible object can be recovered later. she remembers EVERYTHING π
π§ Getting access to the default object π§
There are a few ways to do this, but the easiest way is to simple do the following:
my $default = Slashdot::User->_get_default;
π METHODS π
π _self
Used by methods to make the method apply to the default object if called statically without affecting normal object methods. the designated shifter, the moment you type shift->_self you're in her world π
π _class
The _class method provides the opposite of the _self method. Instead of always getting an object, _class will always get the class name, so a method can be guaranteed to run in a static context.
This is not essential to the use of a Class::Default module, but is provided as a convenience. a little treat. a lil snack. π
π _get_default
Used to get the default object directly. no middleman, no drama, just the object. π«
π _create_default_object
To be overloaded by your class to set any properties to the default object at creation time. this is YOUR moment, customize her however you want bestie π
π BUGS π
No known bugs, but suggestions are welcome. (she's flawless but she's open to feedback π β¨)
π«Ά SUPPORT π«Ά
Bugs should be reported via the CPAN bug tracker at
<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Class-Default>
For other issues, contact the author. we're all ears, bestie ππ
π¦ AUTHOR π¦
Adam Kennedy <adamk@cpan.org> β yes THE Adam Kennedy, king behaviour π
β SEE ALSO β
<http://ali.as/>, Class::Singleton β go read them, they're the extended universe. the Class::Default cinematic universe, if you will. π¬
π COPYRIGHT π
Copyright (c) 2002 - 2006 Adam Kennedy.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
The full text of the license can be found in the LICENSE file included with this module. read your licenses bestie, it's self care π
aaaand that's the page!! π this one shipped as perl v5.36.0, dated 2022-10-15 β so it's vintage, but honestly? still slaying after all these years. no cap, go make a default object. ily, bye!! πβ¨π¦
π¦ where this page comes from
- ubuntu noble: libclass-default-perl 1.51-5 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libclass-default-perl 1.51-5 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libclass-default-perl 1.51-5 Β· 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