As Perl novice I am trying to learn how I can use structs in Perl. In my last question I got some good hints and now I am trying to do this using MooX instead of Class::Struct.

So far it looks quite straightforward to me, not as awkward like most other approaches. However, I surprisingly ran into a problem. Apparently one needs to explicitly declare structs as read/write. Structs in Perl seem by default like constants. Now the big question is how to achieve this.

Here the script that I try to run with perl -w. The script fails with the message: "tags is a read-only accessor at (eval 129) line 17." I have marked line 17 and as you see, it is empty...

use strict; use warnings; use MooX::Struct Document => [ qw($fileID $filename @tags) ]; Document => [ tags => [ is => 'rw', required => 1 ] ]; # the line above produces... # Useless use of a constant ("Document") in void context at testmoo.pl + line 5. # Useless use of anonymous list ([]) in void context at testmoo.pl lin +e 5. # ...instead of making the struct members writeable my @doc_list; # simulate processing two files "by hand" for testing # first document my $filenumber = 1; my $filename = "PetsFile"; my @filetags; # create, allocate and store struct in doc_list my $doc = Document[ $filenumber, $filename, @filetags]; push @doc_list, $doc; # <-- this is line 17, and the script fails with the message: # tags is a read-only accessor at (eval 129) line 17. # second document ++$filenumber; $filename = "BirdsFile"; # create, allocate and store struct in doc_list my $doc = Document[ $filenumber, $filename, @filetags]; push @doc_list, $doc; # simulated processing in a later pass # - this involves modifying struct members after creation my $tag = "Cats"; push @filetags, $tag; $tag = "Dogs"; push @filetags, $tag; $doc_list[1]->tags ( @filetags); my @filetags; # new my frees variable and resets to unde +f again $tag = "Doves"; push @filetags, $tag; $tag = "Eagles"; push @filetags, $tag; $tag = "Shantaks"; push @filetags, $tag; $doc_list[2]->tags ( @filetags); # finally check whether this worked as intended for (1..2) { # my $$doc_pointer = \@doc_list[ @_ ]; # mmh a pointer/hard ref would be nice... # could then avoid to use the index as in the code below... # need to learn how to do this printf "File ID: %s\n", $doc_list[ @_ ]->fileID; printf "Filename: %s\n", $doc_list[ @_ ]->filename; printf "Tags: %s\n", join(', ', @{ $doc_list[ @_ ]->tags }); }

Does anybody have an idea what I could have done wrong?


In reply to How can I make struct members writeable using MooX::Struct? by dissident

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.