Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
I have a few attributes in my class which are instances of other (heavy) classes. I don't want to incur a startup overhead of loading all these heavy classes unless the user accesses the attributes. To illustrate:
I want the 'foo' attribute to behave something like this:package MyClass; use Any::Moose; use Foo::Heavy; # slows startup! has foo => (is => 'ro', default => sub { Foo::Heavy->new });
Is there an easy way to do this in Moose?sub foo { my ($self) = @_; state $foo; if (!$foo) { require Foo::Heavy; $foo = Foo::Heavy->new; } $foo; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Moose/Mouse: assigning default to attribute on first access?
by bobr (Monk) on Jan 11, 2011 at 12:38 UTC | |
|
Re: Moose/Mouse: assigning default to attribute on first access?
by Anonymous Monk on Jan 11, 2011 at 12:31 UTC | |
by Anonymous Monk on Jan 11, 2011 at 14:26 UTC |