Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^5: How to allocate a struct?

by BrowserUk (Patriarch)
on Oct 28, 2014 at 21:20 UTC ( [id://1105375]=note: print w/replies, xml ) Need Help??


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

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.

Replies are listed 'Best First'.
Re^6: How to allocate a struct?
by dissident (Sexton) on Oct 28, 2014 at 22:58 UTC

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

      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.

      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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-19 20:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found