NAME

Test::Builder2::Singleton - A singleton role for TB2

SYNOPSIS

package TB2::Thing;

use Test::Builder2::Mouse;
with 'Test::Builder2::Singleton';

my $thing      = TB2::Thing->singleton;
my $same_thing = TB2::Thing->singleton;

my $new_thing  = TB2::Thing->create;

DESCRIPTION

FOR INTERNAL USE ONLY

A role implementing singleton for Test::Builder2 classes.

Strictly speaking, this isn't a singleton because you can create more instances. Its more like giving the class a default.

METHODS

Constructors

singleton

my $singleton = Class->singleton;
Class->singleton($singleton);

Gets/sets the singleton object.

If there is no singleton one will be created by calling create().

new

Because it is not clear if new() will make a new object or return a singleton (like Test::Builder does) new() will simply croak to force the user to make the decision.

create

my $obj = Class->create(@args);

Creates a new, non-singleton object.

Currently calls Mouse's new method.

make_singleton

my $singleton = $class->make_singleton;

Creates the object used as the singleton.

Defaults to calling $class->create. You can override.

One of the reasons to override is to ensure your singleton contains other singletons. Like a Builder will want to use the singleton History and Formatter objects.