Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

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

Okay, so by convention, you'd have a module with filename "Foo/Bar.pm" and in that you'd define a package called Foo::Bar. That's just a convention though! "Foo/Bar.pm" can define a package called "Foo".

Foo.pm

package Foo; require Foo::Bar; # load additional methods sub aa { my ( $self, $args ) = @_; my $val = $self->_aa ( $args->{'bar'} ); } sub bb { my ( $self, $args ) = @_; my $val = $self->_bb ( $args->{'foo'} ); } 1;

Foo/Bar.pm

package Foo; # not Foo::Bar!!! sub _aa { my ( $self, $quux ) = @_; ...; } sub _bb { my ( $self, $fazoo ) = @_; ...; } 1;

Also, you don't need to do your require Foo::Bar thing right at the top of Foo.pm like I did in my examples. You could do something like:

sub aa { my ( $self, $args ) = @_; require Foo::Bar; my $val = $self->_aa ( $args->{'bar'} ); }

… which will load the additional methods right before they're needed instead of right at the start. If those private methods are uncommonly used (for example, they're used in debugging and diagnostics only, and normal executions of the program don't need them) then this might be a good idea, because you can skip loading, parsing, and compiling those methods most of the time. If the methods are going to be used all/most of the time, you'll get better performance from a single require Foo::Bar at the top of Foo.pm.

I go to even more extremes to split up my CPAN module Types::Standard. Some of the methods defined in that are instead blessed objects overloading coderefification which when they're first called, load their real code from another module, and replace themselves. It's pretty rare that you'd want to go to those extremes, but it makes sense for Types::Standard.


In reply to Re: Breaking up a big module by tobyink
in thread Breaking up a big module by talexb

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 chilling in the Monastery: (4)
As of 2024-04-20 00:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found