dissident has asked for the wisdom of the Perl Monks concerning the following question:
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?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How can I make struct members writeable using MooX::Struct?
by Loops (Curate) on Oct 28, 2014 at 22:27 UTC | |
by dissident (Beadle) on Oct 28, 2014 at 23:27 UTC | |
|
Re: How can I make struct members writeable using MooX::Struct?
by Laurent_R (Canon) on Oct 29, 2014 at 07:37 UTC | |
|
Re: How can I make struct members writeable using MooX::Struct?
by Arunbear (Prior) on Nov 06, 2014 at 17:52 UTC |