Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Need help making the change to OO

by slackert (Novice)
on Jun 27, 2012 at 16:07 UTC ( #978711=perlquestion: print w/ replies, xml ) Need Help??
slackert has asked for the wisdom of the Perl Monks concerning the following question:

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!

Comment on Need help making the change to OO
Select or Download Code
Re: Need help making the change to OO
by dsheroh (Parson) on Jun 27, 2012 at 16:30 UTC
    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...Note: All this example code is written from memory
    If you want help converting your existing code to OO, you're pretty much going to have to post the actual existing code, not paraphrase it from memory.
    None of these work as the object gets 'stringified' ( I guess ) when saved in the hash.
    References get stringified when they are used as the keys of a hash, regardless of whether they're blessed as objects or not. To avoid stringification, store the references as hash values instead. (Note that both of the examples you posted are inserting the Net::CIDR::Lite object as a hash value, so it shouldn't be getting stringified. This leads me to suspect that your actual problem may be something else - but, again, without your actual code, it's impossible to say for sure.)
Re: Need help making the change to OO
by choroba (Abbot) on Jun 27, 2012 at 16:33 UTC
    I do not know what add_cidr returns. Are you sure it returns an object? If not, you should change your code to
    $sites{site1}{ipinfo} = $cidr = Net::CIDR::Lite->new; $cidr->add_cidr('192.168.1.0/24');
Re: Need help making the change to OO
by sauoq (Abbot) on Jun 27, 2012 at 17:06 UTC
    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.

    The thing is, you really seem to be asking for help on how to use someone else's OO module, and that's quite a bit different from "program[ming] in oo style." I don't think you'll find OO sinking in just by converting a small piece of a script you already use to use someone else's OO-ish interface.

    You'd probably be better off (in the learning regard, anyway) by taking some main data structure in a script you already use and refactoring it into a module with an OO interface.

    -sauoq
    "My two cents aren't worth a dime.";
Re: Need help making the change to OO
by CountZero (Bishop) on Jun 27, 2012 at 17:45 UTC
    Why don't you try Moose? It is a great OO building framework.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Re: Need help making the change to OO
by slackert (Novice) on Jun 27, 2012 at 18:25 UTC
    Thanks to everyone for your responses. This was a coding error...I overreacted and assumed that my problem was with the fact that I should be using an oo approach and not what I currently have.

    I was trying to store an object as the key of a hash, not the value.

    I think if my script had been using more oo vs the mix of hohoa madness I currently use, that it would have been easier to work with.

    Just for the record, this is what I ended up using:
    my $cidr = Net::CIDR::Lite->new; $sites{$site}{'ipinfo'}{$cidr->add_any($ip_subnet)};

      Are you sure? And that does what you want?

Re: Need help making the change to OO
by Anonymous Monk on Jun 27, 2012 at 20:22 UTC
    I do not think that you are really trying OO here ... but if you want to "wrap your head around it," it's simple. (Especially in Perl, which "keeps the useful ideas well a hand and skips the academic-only stuff.") An object is a chunk of data which has a chunk of executable code "somehow, magically" associated with it ... such that when you do, "hey you! do this!" the computer can figure out on its own how to "do this." It figures out on its own which subroutine to call. The subroutine that gets called can figure out who "you" is. The key advantage is that the language figures out, on-the-fly every-time right-now at runtime, exactly how to tell "you" to do "this."somehow, magically
Re: Need help making the change to OO
by mrguy123 (Hermit) on Jun 28, 2012 at 05:27 UTC
    As mentioned above, I recommend Moose as an OO framework
    This is a great tutorial that helped me get started.

    Good luck!
    Mister Guy


    About half of the world's greatest inventions were invented by single men trying to impress women. The other half were invented by married men looking for an excuse to get out of the house

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://978711]
Front-paged by stevieb
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (12)
As of 2014-07-24 18:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (165 votes), past polls