Re: Accessor to module

[email protected] (Eric Wilhelm)
Newsgroups perl.moose
Message-ID <[email protected]>
# from Bianka Martinovic
# on Thursday 31 May 2007 08:57 am:

>I realized that the new() method of CM::Module::Loader is called
>whenever I use the accessor. (Well, use in 'submodules' which 'extend'
>the class the accessor is defined in.) This is some overhead I don't
>like in my case. ;)

You mean the default => sub {} is called whenever an object in that 
class or a derived class is created?  That is by-design.

>So, I tried to prevent it by changing the 'default' to:
>
>     default  => sub {
>                     return $CM::Controller::_loader ||= do {
>                         require CM::Module::Loader;
>                         CM::Module::Loader->new; # return of the do
>                     };
>                 },
>
>This works, but I'm not sure if this is the perfect way to do it. I'd
>welcome your suggestions.

You want it to be a singleton that is shared across all objects?  If so, 
you've got a valid solution in the code above.  Another alternative 
would be to construct CM::Module::Loader->new to return this singleton 
(but that changes the policy at a different level.)  There might be 
better singleton support in moose, but I don't have that answer.  (Of 
course, you could just create the object and say "default => $loader".)

You mentioned "overhead", which implies that you're only thinking in 
terms of optimizing?  If so, you can't do that.  If you want each 
object to have a distinct instance of 'loader', you need to create a 
loader every time you create an object, which is what default => sub {} 
usage is intended to support.

--Eric
-- 
The only thing that could save UNIX at this late date would be a new $30
shareware version that runs on an unexpanded Commodore 64.
--Don Lancaster (1991)
---------------------------------------------------
    http://scratchcomputing.com
---------------------------------------------------
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.