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:%sites = { site1 => [ '192.168.1.0/24', '10.10.10.0/24', '4.5.6.7/255.255.255.1 +92'], site2 => [ '1.2.3.4/255.255.255.240', '10.1.1.1/255.255.255.252'] ... };
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.%sites = ( site1 => { ipinfo => [ '192.168.1.0/24', '10.10.10.0/24', '4.5.6.7/255.255. +255.192'], descr => 'North California', isp => 'Verizon' }, site2 => { ipinfo => [ '1.2.3.4/255.255.255.240', '10.1.1.1/255.255.255.252 +'] descr => 'South California', isp => 'Time Warner' }, ... );
I tried things like:
and$sites{'site1'}{'ipinfo'} = Net::CIDR::Lite->new;
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.$cidr = Net::CIDR::Lite->new; $sites{'site1'}{'ipinfo'} = $cidr->add_cidr('192.168.1.0/24');
Anyone out there feel like teaching me to program oo style today?
Thanks!
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Need help making the change to OO
by dsheroh (Monsignor) on Jun 27, 2012 at 16:30 UTC | |
Re: Need help making the change to OO
by sauoq (Abbot) on Jun 27, 2012 at 17:06 UTC | |
Re: Need help making the change to OO
by CountZero (Bishop) on Jun 27, 2012 at 17:45 UTC | |
Re: Need help making the change to OO
by choroba (Cardinal) on Jun 27, 2012 at 16:33 UTC | |
Re: Need help making the change to OO
by Anonymous Monk on Jun 27, 2012 at 20:22 UTC | |
Re: Need help making the change to OO
by slackert (Novice) on Jun 27, 2012 at 18:25 UTC | |
by muba (Priest) on Jun 27, 2012 at 19:11 UTC | |
Re: Need help making the change to OO
by mrguy123 (Hermit) on Jun 28, 2012 at 05:27 UTC |