Q about a module containing more than one class

Discussion in 'Perl Misc' started by Ala Qumsieh, Dec 17, 2003.

  1. Ala Qumsieh

    Ala Qumsieh Guest

    "Michele Dondi" <> wrote in message
    news:...

    > 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
     
    Ala Qumsieh, Dec 17, 2003
    #1
    1. Advertising

  2. Ala Qumsieh

    Ben Morrow Guest

    "Ala Qumsieh" <> wrote:
    > "Michele Dondi" <> wrote in message
    > news:...
    >
    > > 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 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.

    --
    For the last month, a large number of PSNs in the Arpa[Inter-]net have been
    reporting symptoms of congestion ... These reports have been accompanied by an
    increasing number of user complaints ... As of June,... the Arpanet contained
    47 nodes and 63 links. [ftp://rtfm.mit.edu/pub/arpaprob.txt] *
     
    Ben Morrow, Dec 17, 2003
    #2
    1. Advertising

  3. Ala Qumsieh

    Ala Qumsieh Guest

    "Ben Morrow" <> wrote in message
    news:brqahf$jao$...
    >
    > > 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.


    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
     
    Ala Qumsieh, Dec 17, 2003
    #3
  4. 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
    --
    # This prints: Just another Perl hacker,
    seek DATA,15,0 and print q... <DATA>;
    __END__
     
    Michele Dondi, Dec 18, 2003
    #4
  5. Ala Qumsieh

    Ala Qumsieh Guest

    "Michele Dondi" <> wrote in message
    news:...

    > 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
     
    Ala Qumsieh, Dec 18, 2003
    #5
  6. On Wed, 17 Dec 2003 18:01:37 GMT, "Ala Qumsieh"
    <> wrote:

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


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


    >$SOME_PATH/cool/module/foo.pm:
    >$SOME_PATH/cool/module.pm:


    >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
    --
    # This prints: Just another Perl hacker,
    seek DATA,15,0 and print q... <DATA>;
    __END__
     
    Michele Dondi, Dec 19, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. GKalman
    Replies:
    2
    Views:
    278
    GKalman
    May 11, 2009
  2. Merciadri Luca
    Replies:
    4
    Views:
    851
  3. Bob Rashkin
    Replies:
    5
    Views:
    100
    Dennis Lee Bieber
    Dec 23, 2013
  4. Steven D'Aprano
    Replies:
    0
    Views:
    143
    Steven D'Aprano
    Dec 23, 2013
  5. Replies:
    3
    Views:
    116
    Gary Herron
    Dec 23, 2013
Loading...

Share This Page