|There's more than one way to do things|
Using ArrayRef data members in Mooseby dmorgo (Pilgrim)
|on Jul 01, 2008 at 21:31 UTC||Need Help??|
dmorgo has asked for the
wisdom of the Perl Monks concerning the following question:
I'm still fairly new to objects in Perl, having tried several of the approaches recommended in different places (perlboot, perltoot, PBP). Now I'm taking a stab at using Moose for my next project.
I'm getting very frustrated with the Moose documentation. There is certainly a ton of documentation, but it skips from bare basics, up to the clouds, with very little in between. It has a nice Point example, which is simple and great, but then it jumps directly from that into highly arcane examples that have me surfing around on perlpatternswiki reading about Lisp, Haskell, and different theories of programming languages, where I can't tell if the parts talking about Perl were written before, or after, the appearance of Moose.
For Dog's sake, all I want to do is add and remove items to and from an array, where the array is a member variable. Or it can be an arrayref; fine.
But rather than complain any more than I already have, I coded up a simple example of what I'm trying to do. I hope monks wiser than me can point out a better way.
Here is my code. I'd suggest skipping down the the question at the very end first, so you know what part of the code to look at. The first file is lib/MyHolder.pm:
The second file is t/test_holder.t, which exercises the MyHolder object a bit.
Allow me to direct your valuable attention to the last two sections, headed by comments 'add an element' and 'remove an element'. The commented-out lines with simple push() and pop() show what I would *like* to do; the ones below that are what I seem to have to do. The code runs fine; it just seems ugly to have to use a temp array here. Is this really the best way? Please say no.
P.S. There are some MooseX modules like MooseX::AtributeHelpers that offer more options (and lots more arcane programming theory buzzwords, too, not that that's a bad thing). And I definitely will explore those modules. But at the same time I'm still wondering what are the good (simple, short, elegant) ways to work with arrays just with Moose and without extension or experimental modules.