Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: How can I make struct members writeable using MooX::Struct?

by Loops (Curate)
on Oct 28, 2014 at 22:27 UTC ( [id://1105385]=note: print w/replies, xml ) Need Help??


in reply to How can I make struct members writeable using MooX::Struct?

Well the first thing is that along with the use MooX... line you're trying to set two different values for "Document", and have ended up with an unbalanced number of brackets. Perl is giving you some other warnings too, you should work through each of them and post code here that doesn't have any warnings left in it, unless you're really stuck on one of them specifically.

Okay, so here is your program pared down to its minimum, as a first step:

use strict; use warnings; # Create a Document struct with all elements writeable: use MooX::Struct -rw, Document => [ qw( $fileID $filename @tags ) ]; # Create two documents and put them in an array; my $doc = Document[ 1, "PetsFile"]; my $doc2 = Document[ 2, "BirdsFile", ['Doves', 'Eagles']]; my @doc_list = ( $doc, $doc2 ); # Change the tags on both documents to prove they're writeable $doc_list[0]->tags( [qw(Cats Dogs)] ); push @{$doc2->tags}, 'Shantaks'; # Display the result. Notice that array indexes start at 0! for (0..1) { printf "File ID: %s\n", $doc_list[$_]->fileID; printf "Filename: %s\n", $doc_list[$_]->filename; printf "Tags: %s\n", join(', ', @{$doc_list[$_]->tags}); print "\n"; } # But here is a nicer way, without indexes: for my $d (@doc_list) { printf "File ID: %s\n", $d->fileID; printf "Filename: %s\n", $d->filename; printf "Tags: %s\n", join(', ', @{$d->tags}); print "\n"; }

Replies are listed 'Best First'.
Re^2: How can I make struct members writeable using MooX::Struct?
by dissident (Sexton) on Oct 28, 2014 at 23:27 UTC

    Thank you Loops! I read your post and then understood that in the second assignment I didn't just assign a rw property but that it was a complete new assignment. I finally found a way to merge both assignments, but I don't yet know whether they are correct. Then I returned to the monks page and read your update. Didn't know that the rw attribute can be set so easily :)

    Now I am reading your sample and understanding much more... You are showing me the "Perl way" :) Thank you so much!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1105385]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2024-04-18 02:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found