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

Thank you BrowserUk for your suggestion to describe the use-case/the project needs in more detail.

What I want is a tree structure like a directory tree. I'd like to read the text files together with directory-specific configuration files into memory and collect all words marked to be used as search tags (and their aliases). In a second pass all documents are searched again and the tag index is completed. In the third and last pass the web site consisting of the files in the directory tree is built (merging the HTML design templates with the content text files producing html pages, and some generated pages like sitemaps, indexes, galleries and the like).

This is the reason why I'd like to have structs where I can cleanly collect page data, image data, tags and so on.

I could of course use the deprecated BASIC style without any technical problems. But I'd prefer not to use a global array for each and every struct member. Through appropriate naming the code would at least be relatively easy to understand and to modify. But dealing with lots of lists (arrays) of indices to global arrays would be cumbersome. Maybe there indeed exists a "Perl-ish" way to avoid this... but I am still a fresh novice and still need to learn the perlish way of thinking...

Replies are listed 'Best First'.
Re^7: How to allocate a struct?
by BrowserUk (Patriarch) on Oct 28, 2014 at 23:30 UTC
    Maybe there indeed exists a "Perl-ish" way to avoid this...

    The immediate, 'obvious' solution to your problem is a hash. (Or perhaps a tree of hashes; its hard to discern from your brief description.)

    C-structs are simple aggregates of named fields; with the addition that one or more of those named fields can contain pointers to other structs.

    This can be exactly mirrored using hashes; that possibly contain named references to other hashes.

    The down side of this is that hashes are relatively memory hungry and slow when compared to C-style structs; but they smaller and far, far faster than any OO-wrapper to provide the same facility.


    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.
Re^7: How to allocate a struct?
by Anonymous Monk on Oct 29, 2014 at 13:36 UTC

    One of Perl’s great design-strengths is its flexible memory management options and high-performance data structures.   You can, through “references,” string-up all sorts of elaborate contraptions in memory and, for the most part, forget about them.   But one thing that you really don’t use, much if at all, is the C-style notion of a struct.   Instead, you build using hashes, arrays (lists), and, within both of those, references.   There are a lot of perldocs on these topics which people rarely get around to actually reading.