in reply to How to allocate a struct?

Hello dissident, and welcome to the Monastery!

The following line caught my eye:

filename => '@', # @ = array (of char) = string

The comment suggests you may have a misunderstanding. In C, a string is indeed an array of char (i.e., characters). But in Perl, a string is a scalar, not an array. So if you want a single filename, you should say:

filename => '$',

By the way, you might also want to have a look at MooX::Struct by the Monastery’s own tobyink.

Update: For example:

use strict; use warnings; use MooX::Struct Document => [ qw($fileID $filename @tags) ]; my $doc = Document[ 123, 'SampleFileName', [ qw(tag1 tag2 tag3) ] ]; printf "File ID: %s\n", $doc->fileID; printf "Filename: %s\n", $doc->filename; printf "Tags: %s\n", join(', ', @{ $doc->tags });

Output:

22:52 >perl 1068_SoPW.pl File ID: 123 Filename: SampleFileName Tags: tag1, tag2, tag3 22:52 >

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: How to allocate a struct?
by dissident (Beadle) on Oct 28, 2014 at 13:28 UTC

    First, I want to thank you all for welcoming me!

    I didn't expect to get an answer so quickly when I noticed Loops posted the first answer. I did some experiments with the code Loops corrected for me, mainly trying to replace the constants in the example with variables, as the final content of the struct members are not yet known when the struct is created. But I then ran into new problems trying this... Then I noticed two more replies arrived.

    I think perlron's suggestion to thoroughly read perlreftut, perldata, perldsc and perlvar docs could indeed be the key. By the way, I read the class::struct doc several times, but it leaves open a lot of questions. Maybe the docs mentioned will reveal much insight... At least it could help avoiding things like misunderstanding of strings being arrays Athanasius pointed at...

    I'll also look into Moo instead of class::struct, as suggested by Loops and Athanasius.

    Thank you again! Now I'll have to read a lot more documentation and do more experimenting...

      great to see some good posts on monks
      can i also suggest u do a perldoc -f <builtin_command_name> when u dont want to bother with all the info and just u need a quick reminder.
      try it out its fun
      Do not wait to strike when the iron is hot! Make it hot by striking - WB Yeats

        I meanwhile read a lot of perldoc pages and in the perlmonks site. If it were not for the hassle to have to set up a .pm module for each different struct I'd like to use, I'd use the way described in perltoot. But what I want is just basic structs like in old C, without additional OOP features and the like I don't need for my project.

        I feel the lack of structs is really a problem, as there are countless implementations of struct simulations. Mouse, Moose, and all their forks. I really do not understand why the Perl gods don't add structs to Perl itself to make things easier and cleaner.

        So I finally decided to go the way Athanasius suggested in his update. This example of MooX really looks natural and straightforward, compared to the awkwardiness of the examples shown in the docs of the other packages like Moose and so on. But I ran to another problem, on which I probably better post a new question.

        Thanks again to all of you!