Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Breaking up a big module

by tobyink (Abbot)
on Apr 25, 2019 at 13:44 UTC ( #1232986=note: print w/replies, xml ) Need Help??


in reply to Breaking up a big module

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.

Replies are listed 'Best First'.
Re^2: Breaking up a big module
by LanX (Archbishop) on Apr 25, 2019 at 14:04 UTC
    I think thats pretty close to what I described here

    But I'd require inside a BEGIN block or just use use to make sure that non method calls without parens work and other side effects where timing matters.

    The other point is namespace pollution, one might not want such partial "split-modules" to show up in the normal @INC.

    My approach was to add underscores to package names and having a dedicated extra load path.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1232986]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2019-08-25 02:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?