Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Caching a default instance to avoid initializing it every time (with Moo)

by Eily (Prior)
on Feb 08, 2018 at 15:09 UTC ( #1208721=note: print w/replies, xml ) Need Help??


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); }

  • Comment on Re: Caching a default instance to avoid initializing it every time (with Moo)
  • Download Code

Replies are listed 'Best First'.
Re^2: Caching a default instance to avoid initializing it every time (with Moo)
by karlgoethebier (Monsignor) on Feb 09, 2018 at 10:45 UTC
    "...looks like a singleton...I don't know if Moo can do this..."

    Probably this can be easily done with Role::Singleton by just saying with 'Role::Singleton';?

    I never used this and stumbled over it by chance.

    Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

      You'd still have to write a wrapper if you want a single interface to sometimes provide a new instance, sometimes the singleton. But I like that this interface leaves the choice to the caller, you can either have a new instance, regardless of input, or the singleton when you know it can work.

      Ended up going that way in the end. Thanks!

        Nice to hear. Good things need a while (My best Denglish ever) 😎.

        Best regards, Karl

        «The Crux of the Biscuit is the Apostrophe»

        perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Re^2: Caching a default instance to avoid initializing it every time (with Moo)
by Dallaylaen (Hermit) on Feb 16, 2018 at 10:53 UTC

    I ended up going that way because around new wasn't welcome by the team. However, from my point of view caching a read-only object should be transparent to the user of the class as long as it's documented and there's a way to really instantiate a new object if one wants to.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1208721]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (9)
As of 2018-07-16 20:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (349 votes). Check out past polls.

    Notices?