in reply to Recursive Class:Struct syntax error?

What I was trying to do was to define a constant structure containing hard-coded directory and subdirectory names, that would allow me to write nested literal strings. The code would walk this structure forming paths to directories from which it would readdir() the file names. It sounds like Struct doesn't easily support this kind of initialization. Clearly, this can be done by writing a hash-of-hashes-of-hashes as others have suggested, and I eventually used.

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

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

Replies are listed 'Best First'.
Re^2: Recursive Class:Struct syntax error?
by markong (Pilgrim) on Dec 03, 2018 at 23:56 UTC
    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 ;).