Home: Perl Programming Help: Intermediate:
Traveling on a complex object



zatlas1
User

Dec 10, 2014, 8:38 PM


Views: 7235
Traveling on a complex object

I have a structure like the attached (It is an output from MarpaX package). which is a Dumper of the result of this accessor:
my $valueref = $re->value()
With my limited knowledge, I do not know how could I travel on that object to extract, let's say the tuples that belong to all blessed objects of the type 'MarpaX::Languages::SQL::AST::columnDefinition'... in essence, extract all the column names.

Thanks
ZA
Attachments: value1.txt (160 KB)


Laurent_R
Veteran / Moderator

Dec 10, 2014, 11:36 PM


Views: 7224
Re: [zatlas1] Traveling on a complex object

The standard way to access to the inner components of an object is to use the accessor methods of that object's class (or parent classes). Please check the documentation for your object's class.

It is possible in Perl to access directly to the inner components, but this is really not the recommended course of action, because it will fail once the inner implementation of the object will change, whereas the interface (accessor methods) should continue to work.


zatlas1
User

Dec 11, 2014, 3:08 AM


Views: 7220
Re: [Laurent_R] Traveling on a complex object

OK, I know that...
Basically, in my case, I need to code the accessor and I ask for basic help on how to do that
ZA


(This post was edited by zatlas1 on Dec 11, 2014, 3:52 AM)


FishMonger
Veteran / Moderator

Dec 11, 2014, 6:18 AM


Views: 7183
Re: [zatlas1] Traveling on a complex object

So, you're looking to extend the module?

The starting point would be to analyze the source code to learn how the object is built. That will give you a deeper understanding than simply dumping the data structure.


zatlas1
User

Dec 11, 2014, 7:29 AM


Views: 7156
Re: [FishMonger] Traveling on a complex object

Please, I have a brain dead day. I do not need to extend the module, I just get this object and need to code a specialized accessor that would give me what I need.
I cannot recall how to approach it code wise. All I ask is the first basic lines of code that would allow me to access the object. I will do the rest of the work myself and return objects or values as necessary.
Thanks
ZA


FishMonger
Veteran / Moderator

Dec 11, 2014, 8:45 AM


Views: 7137
Re: [zatlas1] Traveling on a complex object

What you're asking has nothing to do with writing "a specialized accessor". You're asking about how to de reference a multi-dimensional array.

If you know the exact array level you need to access, then you can simply access it directly. Otherwise you would use a nested loop to traverse the array.

Doing this on a blessed object instead of using an accessor method provided by the module's API is considered a very poor practice. If the author of the module didn't provide that method, then the proper thing to do is to either extend the module yourself by writing/adding that method to the module (or your subclass) or to ask the author to add that interface in the next release.


zatlas1
User

Dec 11, 2014, 9:11 AM


Views: 7124
Re: [FishMonger] Traveling on a complex object

Thank you
Now I remember how to do that. I can code the accessor(s) and either extend the module or use it in my specialized module.
ZA