UR::Singleton(3pm)
π¦ NAME π¦
okay bestie, welcome to UR::Singleton β it's the abstract class for implementing singleton objects. yes, abstract, meaning it's the blueprint energy, not the thing itself. periodt.
β¨ SYNOPSIS β¨
package MyApp::SomeClass;
use UR;
class MyApp::SomeClass {
is => 'UR::Singleton',
has => [
foo => { is => 'Number' },
]
};
$obj = MyApp::SomeClass->get();
$obj->foo(1);
like... that's it. that's the whole vibe. you declare it, you get() it, you set foo to 1, and the universe is at peace. π
π DESCRIPTION π
so this class right here provides all the infrastructure for singleton classes. and what is a singleton, you ask? it's a class where there can only ever be one instance β no duplicates, no little sibling objects, just the one β and that instance's ID is literally the class name. iconic behavior, honestly. π
if a class inherits from UR::Singleton, it overrides the default implementation of get() and is_loaded() in UR::Object with code that fabricates an appropriate object the first time it's needed. the way that it just... makes the object when you finally need it? no cap, it's giving prepared, it's giving efficient. π§
singletons are most often used as one of the parent classes for data sources within a Namespace. which makes it super convenient to refer to them using only their name β like, just their name, as in a class definition. that's the whole point, that's the flex. πΈ
π METHODS π
π _singleton_object
$obj = Class::Name->_singleton_object;
$obj = $obj->_singleton_object;
returns the object instance whether it is called as a class or object method. either way, you get your object back. equal opportunity caller. π
π _singleton_class_name
$class_name = Class::Name->_singleton_class_name;
$class_name = $obj->_singleton_class_name;
returns the class name whether it is called as a class or object method. same deal, just handing you the name instead of the whole object. π«Ά
π₯ SEE ALSO π₯
UR::Objectβ go read it, bestie, it's the parent energy this whole thing runs on. β
that's the whole page!! go forth and singleton responsibly πβ¨
this lil page is from the User Contributed Perl Documentation, sitting at version perl v5.32.1, dated 2022-01-17. so if you're reading this from the future, hi, hope the singletons still slay. π
πΈ
π¦ where this page comes from
- debian bookworm: libur-perl 0.470+ds-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu jammy: libur-perl 0.470+ds-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libur-perl 0.470+ds-2 Β· 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