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:
{ use strict; package MyHolder; use Moose; has 'name' => (is => 'rw', isa => 'Str'); has 'things' => (is => 'rw', isa => 'ArrayRef'); } 1;
The second file is t/test_holder.t, which exercises the MyHolder object a bit.
#!/usr/bin/perl use lib 'lib'; use strict; use warnings; use MyHolder; use Test::More tests => 7; BEGIN { use_ok( 'MyHolder' ); } require_ok( 'MyHolder' ) or exit; my @temp = (); my $cup_holder = MyHolder->new(); # name $cup_holder->name("cupboard"); is ($cup_holder->name(), "cupboard", "name of cup holder should be cupboard"); # things my @cups = qw(blue green green yellow); $cup_holder->things(\@cups); @temp = @{$cup_holder->things()}; is (scalar @temp, 4, "there should be four things in the cupboard"); # add an element to the array... you've got to be kidding me # push($cup_holder->things(), "red"); @temp = @{$cup_holder->things()}; push(@temp, "red"); $cup_holder->things(\@temp); @temp = @{$cup_holder->things()}; is (scalar @temp, 5, "there should be five things in the cupboard"); # remove the last element from the array... (!) # my $thing = pop $cup_holder->things(); @temp = @{$cup_holder->things()}; my $thing = pop @temp; $cup_holder->things(\@temp); @temp = @{$cup_holder->things()}; is (scalar @temp, 4, "there should be four things in the cupboard"); is ($thing, "red", "the cup should be red");
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.
In reply to Using ArrayRef data members in Moose by dmorgo
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |