VERSION

version 0.29

SYNOPSIS

    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;

DESCRIPTION

A singleton is a class that has only one instance in an application. \*(C`MooseX::Singleton\*(C' lets you easily upgrade (or downgrade, as it were) your Moose class to a singleton.

All you should need to do to transform your class is to change \*(C`use Moose\*(C' to \*(C`use MooseX::Singleton\*(C'. This module uses metaclass roles to do its magic, so it should cooperate with most other \*(C`MooseX\*(C' modules.

METHODS

A singleton class will have the following additional methods:

Singleton->instance

This returns the singleton instance for the given package. This method does not accept any arguments. If the instance does not yet exist, it is created with its defaults values. This means that if your singleton requires arguments, calling \*(C`instance\*(C' will die if the object has not already been initialized.

Singleton->initialize(%args)

This method can be called only once per class. It explicitly initializes the singleton object with the given arguments.

Singleton->_clear_instance

This clears the existing singleton instance for the class. Obviously, this is meant for use only inside the class itself.

Singleton->new

This method currently works like a hybrid of \*(C`initialize\*(C' and \*(C`instance\*(C'. However, calling \*(C`new\*(C' directly will probably be deprecated in a future release. Instead, call \*(C`initialize\*(C' or \*(C`instance\*(C' as appropriate.

BUGS

Please report any bugs or feature requests to \*(C`[email protected]\*(C', or through the web interface at <http://rt.cpan.org>. We will be notified, and then you'll automatically be notified of progress on your bug as we make changes.

SOME CODE STOLEN FROM

Anders Nor Berle <[email protected]>

AND PATCHES FROM

Ricardo \s-1SIGNES\s0 <[email protected]>

AUTHOR

Shawn M Moore <[email protected]>

COPYRIGHT AND LICENSE

This software is copyright (c) 2011 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.