Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^2: Recursive Class:Struct syntax error?

by markong (Pilgrim)
on Dec 03, 2018 at 23:56 UTC ( [id://1226689]=note: print w/replies, xml ) Need Help??


in reply to Re: Recursive Class:Struct syntax error?
in thread Recursive Class:Struct syntax error?

It sounds like Struct doesn't easily support this kind of initialization.

Put simply, in OO parlance, a Struct is a class without methods. Thus you can only define members ("attributes"). To work with structs, as with objects you need to create them, and (maybe contestually) initialize its members.

Your original code was defining a struct *and* trying to initilize its members, which is not supposed to work: using a class you would use a constructor for that task.

Clearly, this can be done by writing a hash-of-hashes-of-hashes as others have suggested, and I eventually used.

Mind that HoH it's not the "perfect" data structure for the job: as bliako has already noted you won't get elements back in any predictable order without counting that now its up to you to walk back and forth the nested maze of elements and this will get surprisingly complex if you need to address the general case: take a look at Data::Walk.

This was obviously not a perfect situation in which to use Struct. :-)

Because you were using the struct as a sort of "root hash element" which does not make much sense: if you do want to learn it, why not use a Tree-Simple whose nodes are Structs?

I bet that, if you are interested in programming, won't be that easy to avoid trees ;).

  • Comment on Re^2: Recursive Class:Struct syntax error?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-25 05:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found