Perl structure flow to C

Viewed 218

I've started working on a program which is in Perl and has to be transformed into C.

There are a lot of subroutines which have structure member accessing which is unfamiliar to me, because I have little to no knowledge about Perl syntax and structure flow. Example:

$ref->{$struct2[$value]->{field1}}->{struct_insideStruct2}->{$ref2->{field}}

$ref is a third structure

$ref2 is a local copy of a parameter which is of type struct1

My question is: How do you create a line like this in C?

Do I need to create nested multiple structures?

I need to understand how multiple access operators in Perl works and if I can create something similiar in C, thanks in advance.

1 Answers

I recommend to not try to directly translate between languages, as this likely results in a clumsy and unnatural code. That would certainly be the case here, as commented further down. The best I can do for this quest is to explain what the expression does

$ref -> { $struct2[$value]->{field1} }
     -> { struct_insideStruct2 }
     -> { $ref2->{field} }

The $ref is a reference to a hash (associative array); it's OK to think of it as a pointer to a hash. One can tell because the -> ("arrow") operator dereferences, and the {...} on its right means that on its left there must be a hash reference; this returns a value that it points to.

In this case, the key with which it is dereferenced (the index into the associative array) involves an element of the array @struct2 at index $value; that element is another hash reference, being dereferenced (indexed into) with a key field1 (string literal†).

What this returns is another hash reference, which is then indexed into (dereferenced) with the key struct_insideStruct2 (string), and this again returns a hash reference.

That last one is indexed with a key which itself is produced by dereferencing another hash reference, $ref2, with a key field (string).

This is an example of a Perl complex data structure. How do you like it? I don't, not very much. Even in Perl, ideally I'd like to see this rewritten as a class, as it goes too deep and wide and so it packs too much complexity without any supporting structure which a class can provide.

If you still wish to indeed and really do that kinda thing in C, you can. May want to find a good hash implementation (or use structs and nest them carefully), and probably to dust off your function pointer syntax and such. But I would recommend to not get into all that.

Instead, once you understand the deep-nested data structure explained above, and the data it represents, find a way to implement what it means and does in your code in a native C way. We always want to use logic, techniques, and idioms native to the language at hand.

Along with linked documentation also see the short and sweet perlintro. The full reference for Perl's references is perlref.


† Normally such "barewords" need be under quotes, 'string' (or using "", or q() or qq() operators ...). But if that is a sole thing between {} then the quoting may be omitted.

Related