Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

OOP in Perl questions. 1

Status
Not open for further replies.

garymgordon

Programmer
Apr 5, 2000
307
US
I need some serious help in understanding some basics, etc. - with regard to OOP in Perl.

I hope someone won't mind working with me on this and helping a real beginner. :) So, in advance, I want to thank (sincerely) anyone who can help. ( I will try and take it one question at a time. )


QUESTION 1:

I read (in a book I have) that objects are defined within modules.

Is this correct? (Can you explain?)

Gary M. Gordon, LLC
webmaster@garymgordon.com
Certified Web Developer ::
Application Programmer
 
OK, here's the run down:


When they say a module, they mean a .pm file. This is the extension of a file using some special stuff that basically means that you can say

use modlueName;
if you have a file called moduleName.pm with the correct contents.

Inside of the module file, you can have several things. Classes and subs. The classes are rules for how to create objects, and the methods can operate on those objects, or not. This sticks with the OOP idea of only allowing (forcing) the programmer using the module to know the interface, rather than the internals, of an object.

That is, definitions for a class, which you can create objects from, go into a module file.

see the h2xs tool for info on generating modlue skelleons.

I hope this is enough info, if not, post again and I'll post more.

Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
I appreciate your help and your explanation was very good. I am still a little unsure.

Are objects ONLY created within modules? Or is it a recommended practice? Or, doesn't it matter?

Are definitions for a class only placed within modules??

Not simply packages within a Perl file??

I'm kind of confused.

Thanks,
Gary


Gary M. Gordon, LLC
webmaster@garymgordon.com
Certified Web Developer ::
Application Programmer
 
Well, the thing with perl is that you can pretty much define an object whereever you want, but it's recommended you create a module (.pm ffile) for several reasons:
1. You're assumed (since you're taking the trouble) to be developing an object that is useful in several situations, otherwise, why use it? A module not only lets you write the class once and just do:
use ModuleName;

2. it also make organization and maintainence much easier.

3. It keeps your implementation seperate from the user (programmer using the class, not end-user). This is the correct OOP way to do it.

4. It hides the implementation of the object, if you use a module, you include it, create it, and use it. No worries about any of the guts of it. This also good OOP practice.

So, a long answer to a short question. You can write objects anywhere in your script, but if you plan on re-using them, you'd be a much more pleasant person to be around if you used modules. I believe I already mentioned this, but h2xs will get you leaps and bounds ahead of null when you start.

Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
Excellent!!!

Gary

COULD YOU TAKE A LOOK AT MY OTHER PERL - OOP QUESTIONS, such as:

OOP: Object definition and constuctor.

Thanks!!!

Gary M. Gordon, LLC
webmaster@garymgordon.com
Certified Web Developer ::
Application Programmer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top