MooseX...ngleton(3pm)

πŸ’… MooseX::Singleton β€” turn your Moose class into a singleton, bestie ✨

okay so first things first: this is MooseX::Singleton, version 0.30, and it lives in the Perl world (user-contributed docs, iykyk). slay.


🌸 NAME 🌸

MooseX::Singleton β€” literally turns your Moose class into a singleton. no cap.


πŸŽ€ VERSION πŸŽ€

version 0.30 β€” that's the vibe check and she passed.


✨ SYNOPSIS ✨

okay so here's the whole mood, copied exactly because code is sacred:

package MyApp;
use MooseX::Singleton;

has env => (
    is      => 'rw',
    isa     => 'HashRef[Str]',
    default => sub { \%ENV },
);

package main;

delete MyApp->env->{PATH};
my $instance = MyApp->instance;
my $same = MyApp->instance;

see how $instance and $same are the same object? that's the whole point, bestie. one instance, main character energy, periodt. πŸ’…


πŸ¦‹ DESCRIPTION πŸ¦‹

so a singleton is a class that has only one instance in an application β€” like there can only be one of her, and honestly? she ate.

MooseX::Singleton lets you easily upgrade (or downgrade, as it were 😭) your Moose class to a singleton. the way that you don't even have to do much:

all you should need to do to transform your class is to change use Moose to use MooseX::Singleton. that's it. that's the whole transformation. iconic.

and because this module uses metaclass roles to do its magic, it should cooperate with most other MooseX modules β€” so no drama, she plays nice with the group chat. 🫢


πŸ’– METHODS πŸ’–

a singleton class will have the following additional methods (bestie got some new tricks, obsessed):

πŸ’œ Singleton->instance

this returns the singleton instance for the given package. it does not accept any arguments β€” don't come at her with args, she's not about that life.

if the instance does not yet exist, it is created with its default values. but plot twist 🫒: this means that if your singleton requires arguments, calling instance will die if the object has not already been initialized. so like… initialize her first, or she will literally perish.

πŸ’œ Singleton->initialize(%args)

this method can be called only once per class β€” one shot, one opportunity, no do-overs. it explicitly initializes the singleton object with the given arguments. use it wisely, queen. πŸ‘‘

πŸ’œ Singleton->_clear_instance

this clears the existing singleton instance for the class. and obviously, this is meant for use only inside the class itself β€” it's private, it's giving lore, don't touch it from the outside, k? πŸ™ˆ

πŸ’œ Singleton->new

this method currently works like a hybrid of initialize and instance β€” a little bit of both, a collab, if you will. ✨

⚠️ but heads up: calling new directly will probably be deprecated in a future release. so instead, call initialize or instance as appropriate. future-proof your code, bestie, it's self-care. πŸ’…

🌈 SOME CODE STOLEN FROM 🌈

Anders Nor Berle <debolaz@gmail.com> β€” shoutout, we love a contributor. 🫢


🌸 AND PATCHES FROM 🌸

Ricardo SIGNES <rjbs@cpan.org> β€” king behavior, no cap. πŸ‘‘


πŸ“ SUPPORT πŸ“

bugs may be submitted through the RT bug tracker at <https://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-Singleton> β€” or you can email bug-MooseX-Singleton@rt.cpan.org at <mailto:bug-MooseX-Singleton@rt.cpan.org>. iykyk. πŸ›

there's also a mailing list available for users of this distribution, at <http://lists.perl.org/list/moose.html> β€” come hang, bestie.

and there's also an irc channel available for users of this distribution, at "#moose" on "irc.perl.org" at <irc://irc.perl.org/#moose> β€” the group chat is literally open, pull up. 🌈


🌟 AUTHOR 🌟

Shawn M Moore <code@sartak.org> β€” the main character who made this happen. ✨


🧚 CONTRIBUTORS 🧚

  • Dave Rolsky <autarch@urth.org>
  • Karen Etheridge <ether@cpan.org>
  • Ricardo SIGNES <rjbs@cpan.org>
  • Kaare Rasmussen <kaare@jasonic.dk>
  • Anders Nor Berle <berle@cpan.org>
  • Jonathan Rockway <jon@jrock.us>
  • Hans Dieter Pearcey <hdp@weftsoar.net>

the whole squad, the whole team β€” they all pulled up. πŸ”₯


this software is copyright (c) 2007 by Shawn M Moore.

this is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. so like… free, but with rules. read the Perl 5 terms, bestie, they matter. πŸ’…


and that's the whole page, bestie. you're now a singleton expert, fr fr. go forth and make sure your class only has one of her. πŸŒˆπŸ‘‘

β€” with love from the docs, perl v5.42.0, dated 2016-11-24 πŸŽ€βœ¨

πŸ“¦ where this page comes from