why does perl's Module::Load::Conditional::check_install find uninstalled module? - perl

--> perl -v
This is perl 5, version 24, subversion 0 (v5.24.0) built for x86_64-linux
I'm trying to conditionally load a perl module if it's installed using...
#!/usr/bin/env perl
use strict;
use Module::Load::Conditional qw[can_load check_install requires];
if(check_install( module => 'dvm_common')) {
use dvm_common;
print "Looks like dvm_common is installed, so it was loaded.\n";
} else {
print "Looks like dvm_common is not installed.\n";
}
exit;
Won't compile, complains about missing module...
--> perl -c ./mod_load_cond_test.pl
Can't locate dvm_common.pm in #INC (you may need to install the dvm_common module) (#INC contains: etc... .) at ./mod_load_cond_test.pl line 6.
BEGIN failed--compilation aborted at ./mod_load_cond_test.pl line 6.
I thought the whole point of using this was to test for the existence of the module before loading it ?

use is a compile-time statement, so the interpreter will load a module immediately when it it passing through the source and encounters a use statement, as if you had written
BEGIN { require Module; Module->import }
To load a module at run-time, you can use require
if(check_install( module => 'dvm_common')) {
require dvm_common;
dvm_common->can("import") && dvm_common->import; # optional
...
or string eval:
if(check_install( module => 'dvm_common')) {
eval "use dvm_common;1" or die $#;
...

Related

Why perl tries to reload module when it is already loaded?

I was trying to install Carton into local directory for my project and got error:
--> Working on Test::Deep
Fetching...
...
t/isa.t ..................... Can't locate Mojo/Base.pm in #INC (you may need to install the Mojo::Base module) (#INC contains: CODE(0x557913d006d0) t/lib /home/kes/.cpanm/work/1626520042.29670/Test-Deep-1.130/blib/lib /home/kes/.cpanm/work/1626520042.29670/Test-Deep-1.130/blib/arch /home/kes/work/projects/tucha/monkeyman/local/lib/perl5/x86_64-linux /home/kes/work/projects/tucha/monkeyman/local/lib/perl5 /home/kes/work/projects/tucha/monkeyman/lib /home/kes/perl5/perlbrew/perls/perl-5.35.1/lib/site_perl/5.35.1/x86_64-linux /home/kes/perl5/perlbrew/perls/perl-5.35.1/lib/site_perl/5.35.1 /home/kes/perl5/perlbrew/perls/perl-5.35.1/lib/5.35.1/x86_64-linux /home/kes/perl5/perlbrew/perls/perl-5.35.1/lib/5.35.1 CODE(0x557913d00670) .) at /home/kes/work/projects/tucha/monkeyman/lib/A.pm line 2.
BEGIN failed--compilation aborted at /home/kes/work/projects/tucha/monkeyman/lib/A.pm line 2.
Compilation failed in require at /home/kes/perl5/perlbrew/perls/perl-5.35.1/lib/5.35.1/base.pm line 138.
...propagated at /home/kes/perl5/perlbrew/perls/perl-5.35.1/lib/5.35.1/base.pm line 160.
BEGIN failed--compilation aborted at t/isa.t line 133.
This error occur because my application has A module, but t/isa.t defines already its own package A which is loaded into memory already.
# Test::Deep:t/isa.t:120
package A;
use Test::Deep;
#A::ISA = qw( Test::Deep );
{
::ok(A->isa("Test::Deep"), "U::isa says yes");
::ok(! A->isa("Test"), "U::isa says yes");
}
{
package C;
use base 'A'; # <<<< this cause error
}
But why use base 'A' tries to reload package A from disk while package A is already loaded into memory?
perl -v
5.35.1
You are experiencing the difference between packages and modules. A package is a namespace. A module is a file. It's easy to confuse them because by convention, the definition of the Foo::Bar::Baz namespace will be in file Foo/Bar/Baz.pm.
When you write:
use Foo::Bar::Baz;
Perl interprets that as two instructions:
Load the file Foo/Bar/Baz.pm if it's not already loaded.
Call the method import on the Foo::Bar::Baz namespace.
(And use base 'Foo::Bar::Baz' does similar, except instead of #2, it does some funky stuff with inheritance.)
So in your case, when you do use base 'A', Perl will do #1 unless file A.pm is already loaded. Yes, you've already defined some stuff in the A namespace, but that doesn't matter.
A few different solutions for you.
Trick Perl into thinking A.pm is already loaded
Adding this line somewhere before use base 'A' will trick Perl into thinking A.pm has already been loaded.
BEGIN { $INC{'A.pm'} = __FILE__ };
use parent
Do this instead of use base 'A':
use parent '-norequire', 'A';
parent is the more modern version of base and has a -norequire option to skip step #1.
use neither base nor parent
All those modules are doing for you are setting the #ISA variable for you. You can do that yourself.
{
package C;
our #ISA = 'A';
}

Does package compiled partially? How to prevent this?

I have code (some lines are removed):
package MaitreD::Command::bank_statement;
use Mojo::Base 'Mojolicious::Command';
sub run {
...
my $payments = read_file( $file ); # line 58
...
}
use XBase; # line 174
sub read_file {
...
}
1;
I run my application. And then do two http requests to this app. Controller runs this command as:
$c->app->commands->run( bank_statement => $upload );
I get next error (this one is expected):
Can't locate XBase.pm in #INC (you may need to install the XBase module) (#INC contains: /opt/monkeyman/lib /opt/monkeyman/local/lib/perl5/x86_64-linux /opt/monkeyman/local/lib/perl5 /opt/monkeyman/lib /opt/monkeyman/local/lib/perl5/5.24.1/x86_64-linux /opt/monkeyman/local/lib/perl5/5.24.1 /opt/monkeyman/local/lib/perl5/x86_64-linux /opt/monkeyman/local/lib/perl5 /opt/perlbrew/perls/perl-5.24.1/lib/site_perl/5.24.1/x86_64-linux /opt/perlbrew/perls/perl-5.24.1/lib/site_perl/5.24.1 /opt/perlbrew/perls/perl-5.24.1/lib/5.24.1/x86_64-linux /opt/perlbrew/perls/perl-5.24.1/lib/5.24.1 .) at /opt/monkeyman/lib/MaitreD/Command/bank_statement.pm line 174.
BEGIN failed--compilation aborted at /opt/monkeyman/lib/MaitreD/Command/bank_statement.pm line 174.
Compilation failed in require at (eval 2620) line 1.
But when I did second request I got different error:
Undefined subroutine &MaitreD::Command::bank_statement::read_file called at /opt/monkeyman/lib/MaitreD/Command/bank_statement.pm line 58.
How MaitreD::Command::bank_statement::run could be run from controller if module MaitreD::Command::bank_statement compilation failed?
If understand correct the module MaitreD::Command::bank_statement was compiled partially to 174 line. So next http request to app can call MaitreD::Command::bank_statement::run and when 58 line is reached I get Undefined subroutine &M::C::b::read_file called because nothing is compiled after 174 line.
How to prevent partial compilation?
I want if there are some errors occur then nothing from MaitreD::Command::bank_statement should be available
It seems you should be focusing on ensuring use XBase actually succeeds, beause presumably it's there for a reason and it's needed for the rest of the program to work.
Why does it fail? Fix that first and the partial compilation isn't a problem.
In this case, why can't perl find the module?
Is it possible the Command::bank_statement class isn't used directly, but only when it's being run, so maybe the current working directory changed between the program start and the time $c->app->commands->run( bank_statement => $upload ); was called?
If that's the case, try loading the command class earlier. e.g. add this to the Mojo application class (probably something like lib/MaitreD.pm:
use MaitreD::Command::bank_statement;

What does this error mean: "import is not exported by the exporter module"?

As the title states, I get this error when trying to use my perl module, but I have no idea what it means and I can't seem to find any clear results on the internet. My code consists of 3 files: a script (myApp.pl) which uses a module (MyLib.pm) which in turn uses another module (Secret.pm). Here they are in their entirety:
myApp.pl
#!/path/to/perl
my $version = "1.0.0";
use warnings;
use strict;
use Testing::MyLib;
MyLib.pm
package Testing::MyLib;
use strict;
use warnings;
use Testing::Secret;
Secret.pm
package Testing::Secret;
use strict;
use warnings;
use Exporter qw( import );
our #EXPORT = ();
our %EXPORT_TAGS = (
'all' => [ qw( MY_CONSTANT )]
);
our #EXPORT_OK = (
#{ $EXPORT_TAGS{all}}
);
use constant MY_CONSTANT => 'bla bla bla';
They exit in this file structure:
/bin/myApp.pl
/lib/perl/Testing/MyLib.pm
/lib/perl/Testing/Secret.pm
And the detail of the error message is:
[user#pc ~]$ myApp.pl
"import" is not exported by the Exporter module at /###/lib/perl/Testing/Secret.pm line 6
Can't continue after import errors at /###/lib/perl/Testing/Secret.pm line 6
BEGIN failed--compilation aborted at /###/lib/perl/Testing/Secret.pm line 6.
Compilation failed in require at /###/lib/perl/Testing/MyLib.pm line 6.
BEGIN failed--compilation aborted at /###/lib/perl/Testing/MyLib.pm line 6.
Compilation failed in require at /###/bin/myApp.pl line 7.
BEGIN failed--compilation aborted at /###/bin/myApp.pl line 7.
use Exporter qw( import ); requests that Exporter exports (creates) import in your module's namespace. This is the method that handles requests to export from your module. Versions of Exporter older than 5.57 don't recognize this request, leading to the error message you obtained.
Since Exporter 5.57 or newer has been bundled with Perl since Perl 5.8.3, you must have a pretty ancient version of Perl and the module!
You could upgrade Exporter, or you could inherit import from Exporter, which is a little messier but works with any version of Exporter.
package MyPackage;
use strict;
use warnings;
use Exporter;
our #ISA = 'Exporter';
our #EXPORT_OK = ...;

Mojolicious + MongoDB: Can't locate MongoDB.pm error

My app crashes each time it includes as much as "use MongoDB;" in my perl app file.
I have installed MongoDB successfully. I can check my databases use one or the other, check for collections, create new collections, all from the shell.
If I try to connect to mongoDb from mojolicious app like:
!/usr/bin/env perl
use Mojolicious::Lite;
use MongoDB;
use MongoDB::OID;
my $mongo_port = shift || 27017;
helper 'mongo' => sub {
my ($self, $name) = #_;
my $host = 'localhost:' . $mongo_port;
my $conn = MongoDB::MongoClient->new(host => $host);
my $db = $conn->get_database('test');
};
helper 'value2oid' => sub {
my ($self, $value) = #_;
MongoDB::OID->new($value);
};
If I have a working app and include as much as :
Use MongoDB;
I get:
Can't load application from file "/Users/eevitomperi/Desktop/Programming/Perl/mojoliciousApp/foodAbout/app.pl": Can't locate MongoDB.pm in #INC (you may need to install the MongoDB module) (#INC contains: /Library/Perl/5.18/darwin-thread-multi-2level /Library/Perl/5.18 /Network/Library/Perl/5.18/darwin-thread-multi-2level /Network/Library/Perl/5.18 /Library/Perl/Updates/5.18.2/darwin-thread-multi-2level /Library/Perl/Updates/5.18.2 /System/Library/Perl/5.18/darwin-thread-multi-2level /System/Library/Perl/5.18 /System/Library/Perl/Extras/5.18/darwin-thread-multi-2level /System/Library/Perl/Extras/5.18 .) at /Users/eevitomperi/Desktop/Programming/Perl/mojoliciousApp/foodAbout/app.pl line 4.
BEGIN failed--compilation aborted at /Users/eevitomperi/Desktop/Programming/Perl/mojoliciousApp/foodAbout/app.pl line 4.
I am completely new to mongo, mojolicious and perl so I guess I did not install some package?
Does the MongoDB files(mongo, mongod....) have to be within the mojolicious project ?
Not sure what I am missing and all documentation starts with the use of "Use MongoDB;" within mojolicious app so not sure what to do.
Hopefully someone can point out what I missed.
Install module:
cpanm Mojolicious::Plugin::Mongodb
Fix the following:
Can't write to /Library/Perl/5.18 and /usr/local/bin: Installing modules to /Users/eevitomperi/perl5
! To turn off this warning, you have to do one of the following:
! - run me as a root or with --sudo option (to install to /Library/Perl/5.18 and /usr/local/bin)
! - Configure local::lib your existing local::lib in this shell to set PERL_MM_OPT etc.
! - Install local::lib by running the following commands
By running:
cpanm --local-lib=~/perl5 local::lib && eval $(perl -I ~/perl5/lib/perl5/ -Mlocal::lib)
Now I can connect to Mongo from mojolicious

How to calculate time average from a array list

I have a time array list in format h:mm:ss. I want to calculate the average time of this array.
I tried below code but there is some problem with the packages. I successfully installed Duration package but now complier is throwing error on Duration::Parse package.
PS: I am using Dwinperl as the editor on windows.
use Time::Duration::Parse qw(parse_duration);
use Time::Duration qw(duration);
use List::Util qw(sum);
my $count = #time;
my $sum = sum map {parse_duration($_) } #time;
my $avg = $sum / $count;
print duration($sum, 3), "--Total Time\n";
print duration($avg, 3), "--Avg Time\n";
this is the error message that I am getting.
Can't locate Time/Duration/Parse.pm in #INC (#INC contains: C:/Dwimperl/perl/sit
e/lib C:/Dwimperl/perl/vendor/lib C:/Dwimperl/perl/lib .) at time.pl line 7.
BEGIN failed--compilation aborted at time.pl line 7.
It seems perl could not find package Time::Duration::Parse installed within list of #INC paths
Type following command in terminal to check if perl can find your module by default
perldoc -l Time::Duration::Parse
If above command didn't give you installed location of desired module,
Make sure you have installed required modules
try adding following line to your perl code to add custom installed path of module
use lib '/path/to/module';