I am considering moving the individual methods of a module into separate modules in order to obtain more manageable files. I wrote a little test for this:
a.pl:
#!/usr/bin/perl
use 5.028;
use warnings;
use utf8;
use open ':std', ':encoding(UTF-8)';
use Readonly;
use English qw(-no_match_vars);
use Benchmark qw(:all);
use A;
our $VERSION = 1;
Readonly::Scalar my $COUNT => 10_000_000;
warn $A::VERSION;
warn $A::Login2::VERSION;
my $a = A->new;
warn $a;
$a->login(1);
$a->login2(1);
cmpthese($COUNT, {
login => sub{$a->login},
login2 => sub{$a->login2}
});
A.pm:
package A;
use 5.028;
use warnings;
use utf8;
use open ':std', ':encoding(UTF-8)';
use Readonly;
use English qw(-no_match_vars);
use A::Login2 'login2';
our $VERSION = 1;
sub new {
my ($class, $p) = @_;
my $this = {};
bless $this, $class;
return $this;
}
sub login {
my ($this, $dump) = @_;
if ($dump) {
warn "$this: login";
$this->test;
}
return;
}
sub test {
my ($this) = @_;
warn "$this: test";
return;
}
1;
A/Login2.pm:
package A::Login2;
use 5.028;
use warnings;
use utf8;
use open ':std', ':encoding(UTF-8)';
use Readonly;
use English qw(-no_match_vars);
use base 'Exporter';
our @EXPORT_OK = qw(login2);
our $VERSION = 1.1;
sub login2 {
my ($this, $dump) = @_;
if ($dump) {
warn "$this: login2";
$this->test;
}
return;
}
1;
The output from ./a.pl is:
1 at ./a.pl line 18.
1.1 at ./a.pl line 19.
A=HASH(0x5581d48f3470) at ./a.pl line 21.
A=HASH(0x5581d48f3470): login at A.pm line 25.
A=HASH(0x5581d48f3470): test at A.pm line 34.
A=HASH(0x5581d48f3470): login2 at A/Login2.pm line 18.
A=HASH(0x5581d48f3470): test at A.pm line 34.
Rate login login2
login 5847953/s -- -6%
login2 6250000/s 7% --
I would have thought that login was faster than login2.
- Why login2 is faster than login?
- Is it a good Idea to put each method in its own module?
- Is there a better way?
I'm looking forward for comments.