Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-03-28 15:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found