Hello,

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.

%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'] ... };
That eventually changed to hold other data about the sites:
%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' }, ... );
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:

$sites{'site1'}{'ipinfo'} = Net::CIDR::Lite->new;
and
$cidr = Net::CIDR::Lite->new; $sites{'site1'}{'ipinfo'} = $cidr->add_cidr('192.168.1.0/24');
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?

Thanks!


In reply to Need help making the change to OO by slackert

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.