in reply to Caching a default instance to avoid initializing it every time (with Moo)
It kinda looks like a singleton, except not exactly so I don't know if Moo can do this. But maybe since what you want is neither of the two behaviours you might expect for a class (always a new instance, or always the same), this can be done in a separate function, rather than the constructor?
my $default; sub get_instance { my ($class, %args) = @_; return $default ||= $class->new unless %args; return $class->new(%args); }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Caching a default instance to avoid initializing it every time (with Moo)
by karlgoethebier (Abbot) on Feb 09, 2018 at 10:45 UTC | |
by Eily (Monsignor) on Feb 09, 2018 at 10:52 UTC | |
by Dallaylaen (Chaplain) on Jul 01, 2018 at 11:22 UTC | |
by karlgoethebier (Abbot) on Jul 01, 2018 at 11:50 UTC | |
Re^2: Caching a default instance to avoid initializing it every time (with Moo)
by Dallaylaen (Chaplain) on Feb 16, 2018 at 10:53 UTC |