Does package compiled partially? How to prevent this? - perl

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;

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';
}

How to resolve a "Metadata error: chr must be valid" error on Linux?

I am relatively new to the world of coding, so I am having trouble resolving an issue when running TranslocWrapper.pl tutorial_metadata.txt preprocess/ results/ --threads 2. I am trying to run the HTGTS Pipeline according to this GitHub project. This is the full error:
. Library Genome Chr Start End Strand
1 RAG1A_SRep2 hg19 chr11 36594878 36595030 -
Metadata error: chr must be valid at /home/micah/transloc_pipeline/bin/TranslocWrapper.pl line 285.
main::check_validity_of_metadata('HASH(0x2903ac8)') called at /home/micah/transloc_pipeline/bin/TranslocWrapper.pl line 248
main::read_in_meta_file() called at /home/micah/transloc_pipeline/bin/TranslocWrapper.pl line 90
I have already double-checked the successful installation of the Software Dependencies, so everything should be all good, but I am having trouble interpreting the "Metadata error: chr must be valid at ..." line. If it helps, these are the specific lines that are being called in the error:
TranslocWrapper.pl line 285:
croak "Metadata error: chr must be valid" unless grep { $_ eq $expt->{chr} } #chrlist;
TranslocWrapper.pl line 248:
check_validity_of_metadata($expt);
TranslocWrapper.pl line 90:
read_in_meta_file;
Thanks in advance for the help!
So the error is saying that one of the sequence characters in the metadata file is not present in the sequence's assembly file.
Given that this is the provided example you should assume that the data is correct and your invocation is faulty.
Have you done the TranslocPreprocess.pl preprocessing steps?
If you have try looking at the first line of the metadata file, identify the assembly entry. Ensure that the assembly file exists and that it contains the required sequence.
One common problem with this kind of code is the case of the filenames. The examples are designed to be run in Linux where filename case matters. Windows likes to pretend that case doesn't matter, this can cause problems. If you are running this code from Microsoft Windows or extracted any of the archives from within Windows this is a likely cause of the error.

Undefined subroutine &main::remove_tree call

I am writing a perl script that would send email about a the number of open cases and at the sametime will delete a directory which is more than 30 days old.
$age = -M;
if($age > 30)
{
remove_tree ($_);
}
This is what I am doing.Teh script was running fine but after 30 days It stopped running,not deleting the old directories and throws the following error.
Undefined subroutine &main::remove_tree called at /var/www/cgi-bin/remedy-case-
management/remedy-open-cases-script.pl line 35.
Not sure How i solve this.
I removed the paranthesis from remove_tree line and it throws error as follows.
Can't call method "remove_tree" without a package or object reference at /var/www/cgi-bin/remedy- case-management/remedy-open-cases-script.pl line 35.
Can anyone let me know what error i am making here?Thanks.
remove_tree is a function in File::Path.
Be sure to include that module before using the function:
use File::Path qw(remove_tree);
It seems you removed or commented out a line similar to
use File::Path qw{ remove_tree };
See File::Path.

Dancer DBIC Could not load schema_class

I have a Dancer app that is working. I am using DBIC for the database.
I want to add some tables to the database, and so I created new files for those. However, now when I restart the app, I get the following errors
Error while loading ././bin/app.pl: Could not load schema_class MyApp::Schema at /usr/local/share/perl/5.14.2/Dancer/Plugin/DBIC.pm line 42.
Compilation failed in require at ././bin/app.pl line 5.
BEGIN failed--compilation aborted at ././bin/app.pl line 5.
When I remove the new files, everything works fine.
Here is my Schema.pm file
package MyApp::Schema;
# Created by DBIx::Class::Schema::Loader
# DO NOT MODIFY THE FIRST PART OF THIS FILE
use strict;
use warnings;
use base 'DBIx::Class::Schema';
__PACKAGE__->load_namespaces;
1;
Where can I get more information about the error and what I did wrong?
Since you are using __PACKAGE__->load_namespaces, all new classes will loaded, which explains why the simple existence of the new class files cause an error. If there is a syntax error in your new class files, then loading the schema will die.
You can use perl -c on the new class files to ensure they will compile and get a better idea of the problem if they do not.

I keep getting failures installing modules at BEGIN { plan tests => 5 }. What do I need to get past this?

The module it's failing to install is JSON::XS. Really it's failing to install anything
that has the following code:
BEGIN { plan tests => 5 };
From the build.log:
syntax error at t/04_dwiw_encode.t line 13, near "plan tests"
The offending line:
13 BEGIN { plan tests => 5 }
I read that there's a problem with Test.pm but there are quite a few modules
using it and furthermore this just started happening recently.
I just tried reinstalling perlbrew and also tried updating outdated modules
but I keep getting the same failures.
Any one have an idea what might have caused this and how to fix it?
I suspect you either have an older-than-expected version of the Test module, or you created your own module named Test.pm and it's getting picked up instead of the expected module.
You can address the first issue by upgrading Test.
cpan Test
You should address the second issue by renaming your Test.pm to something else, but you might also be able to address it by changing directory and temporarily clearing the PERL5LIB env var.
pushd / ; PERL5LIB= cpan JSON::XS ; popd