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:

{ 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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.