In short: no, not exactly.. :o) I'll try to explain my grasp of the concepts as best as I can..

First of all, OOP is not *only* about reuse. Reuse is a good side effect of using objects, but there are other good effects too..

If you're doing a particular job over and over again, the first place I'd look is to move that code into a sub (subroutine)... Saves overhead, avoids duplicating code all over the place..

OOP deals more with concepts of modularization and abstraction.. Firstly, take the following example:
You have a function which maintains a bank balance. If all you need to do is subtract and/or add amounts from the bank balance, two simple functions named "add_to_account" and "subtract_from_account" would be fine... Pass in the balance, and the amount to add or subtract, and you get a result...However, suppose you also want to maintain the state of the bank balance inside the function ? if you don't want to maintain the balance in your application, then an OOP system is good...

Objects can help you maintain state... to follow the same example:

The procedural method: $balance = add_to_acct($balance, $amount); The OOP method: my $acct_obj = new BankAccount($initial_balance); $acct_obj->add_to_acct($amount);

In other words, what's happened here is that the explicit maintenance of a bank balance has been abstracted away from you.. The object maintains the state of the bank balance... Think of an object as a neat little package which contains all the variables and function calls you need to do a particular job..

As I understand your problem, if you just need to open a file frequently, then an OOP interface is probably overkill.. for your second question, if you want something like this:

add_value($x); add_value($y); add_value($z); my @all_values = get_all_values;
then an object oriented interface looks nicer, but isn't really necessary... you could write it procedurally as:
@all_values = add_value($x, @all_values); # gives the list to add to, and takes the newly added list # as a retu +rn value @all_values = add_value($y, @all_values); ... # Now anytime you inspect @all_values, it has the currently # added el +ements.

Most of this is probably oversimplified.. a look at perltoot,perlsub or searching for object oriented tutorials will probably explain things better...

HTH


In reply to Re: Modular Code by tinman
in thread Modular Code by Stamp_Guy

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.