On Tue, Nov 10, 2009 at 6:09 PM, Markx.Allen <[email protected]> wrote:
> I've read through almost all of the Moose docs I could find on CPAN and
> several of the Moose slide decks floating around the Interwebs.
>
> That being said I have some basic questions that probably have simple
> answers. Maybe I didn't read the fine manuals closely enough, so if I did
> overlook something, please point me toward the right spot in the docs and
> I'll figure it out.
>
> That being said here are my questions:
>
> 1) In the "old style" OO (which I have used for a long time now) I would do
> something like this:
>
> I would make a file called "Account.pm" and try to encapsulate all of the
> functionality of accounts in that one file.
>
> I would make another file called Server.pm and encapsulate functionality
> around servers in the other file. Then if Account and Server have a
> relationship, I would do this:
>
> package Account;
>
> use Server;
>
> I found that if I do this:
>
> package Server; # (in a file called Server.pm)
> use Moose;
>
> has 'name' => {
> is => 'ro',
> isa => 'Str',
> };
>
> # etc
>
> package Account; # (in a file called Account.pm)
> use Moose;
> use Server;
>
> has 'server' => {
> is => 'ro',
> isa => 'Server',
> };
>
> perl -wc Account.pm throws an error
>
> but if I change "use" to "require Server;" I do not get an error.
>
> Why doesn't use work?
That does work, there are several examples on CPAN of where it *has*
to work since that is shipped code. You'll need to provide more
details about the error you're getting for help figuring out what is
going on.
> 2) Maybe I haven't really grokked the Moose Way yet, but what's the best way
> to implement a collection of Moose Objects?
>
> In the old style OO, I would implement an object as, say, Account.pm and then
> I would create a collection class of that object type called XXXCollection or
> AccountCollection.pm in this case. I usually stored individual objects as a
> hash of hashes, using the object name as the key.
That would still work. The simplest possible solution is an ArrayRef or HashRef
package Server;
use Moose;
has accounts => ( isa => 'ArraRef[Account]', is => 'ro', ...); #
# then later
Server->new( accounts => [Account->new( ...) ] );
The packages in the rather new Native Attributes code can help here.
If you have a more specific question someone can help sort it out, but
you're asking a very generic question so I can only provide a very
generic answer.
> I suspect that Roles and Traits can help me write a Collection class
> trivially where I will get a lot of functionality "for free" but I'm just not
> quite sure I understand the docs well enough to put it all together.
Roles can provide a collection API if you want to go that route. A
quick example would be something like:
package MyApp::Collection::API;
use Moose::Role;
has accounts => (
isa => 'ArraRef[Object]',
is => 'ro',
traits => ['Array']
handles => { add_element => 'push', remove_element => 'pop' }
);
package AccountCollection;
use Moose;
use MooseX::Aliases;
with qw(MyApp::Collection::API);
alias add_account => 'add_element';
alias rmove_account => 'remove_element';
But again It'd help to have specific questions if you want more
specific answers :)