What is a good way to reorganize monsters of a Perl module into submodules?

I have a Perl module for a project. I can have a dozen programs that depend on it, and many of them are garbage. Before, I did not spend much time with DBI, so the part is fixed, but the main thing is that it is big. Literally 2KLOC.

It would be easy to break this function (let's call it Dumb.pm) into separate modules (Dumb :: FormTools, Dumb :: Database, etc.), except, as I said, many programs, use Dumb; ''

I would like to export Dumb :: Database exported functions through Dumb without having to repeat this over and over:

sub my_dumb_function { return Dumb::Database::my_dumb_function( @_ ) ; }

This does not mean that I am above this. It just seems like a stupid and inelegant way to solve this problem. Once I used the excuse “I don’t know anything better,” and once it really is more than you. Help?

+5
source share
4 answers

You don’t know how you are using it now (is it currently using export methods?), But you can configure new child modules so that you can import your functions (using Exporter) and then just import the original module now broken pieces. Sort of:

package Dumb;

use Dumb::Database qw(my_dumb_function);

1;

package Dumb::Database;

use base qw(Exporter);

our @EXPORT_OK = qw(my_dumb_function);

sub my_dumb_function { 1; }

1;
+3
source

, . 500- -, . , .

  • . .
  • , . . , . , , .
  • . , - .

, , , , " , Dumb?". import, Exporter import_to_level. , , . , Dumb::Database import , Dumb, Dumb, Dumb::Database.

+7

, Dumb.pm Exporter. , ( ), @EXPORT, .

package Dumb;
use Dumb::FormTools ':all';
use Dumb::Database  ':all';

use Exporter 'import';

our @EXPORT = ...;    # Unchanged from original version
our @EXPORT_OK = ...; # Unchanged from original version

1;

:all . ( ).

our %EXPORT_TAGS = ( all => [ @EXPORT, @EXPORT_OK ] );
# or, for a module that doesn't export anything by default:
our %EXPORT_TAGS = ( all => \@EXPORT_OK );

On the other hand, if the submodule has no functions @EXPORT_OK, you can skip the tag :alland just say it use Dumb::Submodule;.

+3
source

You can also watch Sub :: Exporter

+1
source

All Articles