Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: Modular Code

by tinman (Curate)
on Jun 14, 2001 at 22:37 UTC ( #88564=note: print w/replies, xml ) Need Help??

in reply to Modular Code

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...


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://88564]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2017-02-22 22:57 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (338 votes). Check out past polls.