in reply to Re: How to allocate a struct?
in thread How to allocate a struct?

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...

Replies are listed 'Best First'.
Re^3: How to allocate a struct?
by perlron (Pilgrim) on Oct 28, 2014 at 14:36 UTC
    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!

        I really do not understand why the Perl gods don't add structs to Perl itself to make things easier and cleaner.

        One good reason is that once you understand the facilities of Perl, for any given use-case where those coming from other languages might use a struct, there is nearly always a better, more perlish approach to solving the requirement.

        So, rather than ruing the lack of some facility that, even if someone started the process of adding that facility tomorrow, wouldn't be available for months if not years, why not describe your use-case in detail, and see if the monks can't offer you some alternative that is available now?


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.