How to calculate time average from a array list - perl

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

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;

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

--> 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 $#;
...

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.

Open2 Api failure while installing Mead software

I am running into some issues trying to install a software called MEAD . I would appreciate if someone could have alook .
I get the following error while installing
/mead/bin # ./mead.pl GA3
Using system rc-file: /home/karosh/mead/bin/../.meadrc
Warning: Can't find user rc-file
Cluster: /home/karosh/mead/bin/../data/GA3/GA3.cluster
open2: exec of /home/karosh/mead/bin/driver.pl failed at ./mead.pl line 230
THe mead software is not written by me so I have not changed any of the perl scrips . I line 230 in the driver.pl file is
sub run_mead {
my %options = #_;
my $reader = FileHandle->new();
my $writer = FileHandle->new();
unless ( open2($reader, $writer, "$FindBin::Bin/driver.pl") ) {
die "Unable to run MEAD.\n";
}
...
...
}
Does this error mean that open2 was not found . The mead folks have put the following line in the file:
use strict;
use File::Spec;
use FileHandle;
use IPC::Open2;
Or does it mean that i need to install the rpm that contains the API . I see that this API is a part of the core perl bundle http://perldoc.perl.org/IPC/Open2.html. So why was it not installed ? Do i need to install perl again .
Someone has earlier faced this problem - http://www.summarization.com/~radev/mead/email/0160.html but the solution is not working for me . I find no Perl files with the incorrect perl directives . The mead team has been dissolved and there is no one to ask questions but I need to use this software.
I think if some one can explain me the meaning of the error than I can do deeper. Anyone?
It probably means that .../driver.pl doesn't have execute permission. Change the file permissions or call it like
open2($reader, $writer, "perl $FindBin::Bin/driver.pl")
open2($reader, $writer, "$^X $FindBin::Bin/driver.pl")