Q about a module containing more than one class

A

Ala Qumsieh

I have a class that relies on other classes (as suggested in many
docs) that are to be used *exclusively* for this, i.e. not as
standalone classes.

Now I want to put all these classes in the same (separate) module so
that only the "main" one is made (explicitly) available to the final
user, and my question is wether there's any convention/direction for
assigning a name to the "other" classes:

I guess different people would do it differently. I would make is such that
each module is in a separate file, and would name them such that each '::'
in the module name corresponds to a real directory hierarchy. You don't have
to do that of course, but I find it easier to look for files, and understand
what class uses what. Something like this (untested):

$SOME_PATH/cool/module/foo.pm:

package cool::module::foo;

sub new { ... }

$SOME_PATH/cool/module.pm:

package cool::module;
use lib $SOME_PATH;
use cool::module::foo;

sub new { ... }

That will make it easier if you want to use cool::module::foo in some other
class as well.

--Ala
 
B

Ben Morrow

I would tend to name them under the namespace of the main class, so a
util class for My::Class would be called My::Class::Util. It is a
*very* bad idea to use a random top-level name: package names are
global, remember, so you open yourself up to clashes. This applies
whether you put all the classes into one module or split them up into
separate modules that you main module uses: which is often a good idea.
I guess different people would do it differently. I would make is such that
each module is in a separate file, and would name them such that each '::'
in the module name corresponds to a real directory hierarchy. You don't have
to do that of course, but I find it easier to look for files, and understand
what class uses what. Something like this (untested):

You seem to have missed the whole point of 'require'... anyone who
keeps their Perl modules organised any differently from this is either
a fool or knows something I don't.
 
A

Ala Qumsieh

Ben Morrow said:
You seem to have missed the whole point of 'require'... anyone who
keeps their Perl modules organised any differently from this is either
a fool or knows something I don't.

No, I know exactly what require() and use() do. But, the OP had all the
classes in one file, in which case no require() is, well, required. Perhaps
my sentence wasn't completely clear. When I said "you don't have to do
that", I was referring to my previous sentence (all of it). If you decide
not to do that, then the OPs approach is fine, if you're so inclined.

And, in my example, I made sure I had a 'use ...' in the top most class.

--Ala
 
M

Michele Dondi

I apologize in advance if this is a bogus question. I'm just starting
to understand Perl's OO programming.

I have a class that relies on other classes (as suggested in many
docs) that are to be used *exclusively* for this, i.e. not as
standalone classes.

Now I want to put all these classes in the same (separate) module so
that only the "main" one is made (explicitly) available to the final
user, and my question is wether there's any convention/direction for
assigning a name to the "other" classes:


# $SOMEWHERE_IN_LIB/cool/module.pm
package foo;
# or would it be better to call it 'cool::module::foo'?

...

package cool::module;

sub new {
my $class = shift;
...
bless {_this => [...],
_foo => foo->new(...) }, $class;
}

...

1;
__END__


TIA,
Michele
 
A

Ala Qumsieh

This is *not* what I want because I do *not* want to use
cool::module::foo in other classes.

Ahh .. I misunderstood your question.
It wouldn't be a *problem* to do it the way you suggest anyway, but I
*prefer* to hide (for this very weak meaning of "hide")
cool::module::foo in $SOME_PATH/cool/module.pm.

Since your foo module is exclusive for cool::module, I would stick with the
cool::module:: hierarchy. For me, this would make it clear that the two
modules are related, and it avoids polluting other namespaces as well.

--Ala
 
M

Michele Dondi

I guess different people would do it differently. I would make is such that
each module is in a separate file, and would name them such that each '::'

That will make it easier if you want to use cool::module::foo in some other
class as well.

This is *not* what I want because I do *not* want to use
cool::module::foo in other classes.

It wouldn't be a *problem* to do it the way you suggest anyway, but I
*prefer* to hide (for this very weak meaning of "hide")
cool::module::foo in $SOME_PATH/cool/module.pm.


Michele
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top