|Perl Monk, Perl Meditation|
Need help making the change to OOby slackert (Novice)
|on Jun 27, 2012 at 16:07 UTC||Need Help??|
slackert has asked for the
wisdom of the Perl Monks concerning the following question:
I have been struggling trying to finally ditch all of my hoaohoaoaoh ugliness in my scripts and finally embrace oo. I'm struggling though. I've read through many tutorials but I just can't seem to wrap my head around oo. I'm hoping that if someone could help me convert a small piece of a script I already use to oo that it'll finally sink in.
Currently I've got a small pice of code that I used for keeping track of IP subnets at many of our sites. I used a hash like this:
Note: All this example code is written from memory, I'm sure there are syntactical errors. It should suffice to make the point though.
That eventually changed to hold other data about the sites:
To me I already notice the code getting kind of ugly, parsing through that is not elegant and it's not particularly easy to add 'fields' to. I have left some of the 'fields' out for the sake of brevity. When updating part of my script that processes the ip subnet info I wanted to replace the ipinfo array with Net::CIDR::Lite objects, however, saving objects in a hash didnt work out so well. Net::CIDR works much much better than what I was doing.
I tried things like:
None of these work as the object gets 'stringified' ( I guess ) when saved in the hash. I'm not sure if I should just be using references to an object or totally ditch my hohoa structures and migrate to oo.I feel like I'm just on the cusp of getting oo, just need some help.
Anyone out there feel like teaching me to program oo style today?