Perl: cannot find object methods panel via package

I am new to this site, so bear with me if this question has already been answered elsewhere. I try to call the subroutine "bar" from the module "codons1.pm", and I encounter the error: Can't find the method of the object "bar" through the package "codons1.pm" (maybe you forgot to download "codons1.pm"?). The main script is as follows:

use strict;
use warnings;
my $i = 1;
my $pack = "codons$i\.pm";
require $pack;
(my %temp) = $pack->bar();
print keys %INC ;

Thanks ( Perl Object Error: Unable to find object method via package ), I was able to verify using% INC that the module was loaded. The module is as follows:

package codons1;
sub bar{ #some code; 
return (%some_hash);}
1;

I use $ i so that I can load several similar modules through a loop. Any suggestions are welcome and very grateful in advance.

+4
2

codons1, codons1.pm->bar. :

my $pack = "codons$i";
require "$pack.pm";
$pack->bar();

my $pack = "codons$i";
eval "require $pack";
$pack->bar();
+4

,

#!/usr/bin/perl
use strict;
use warnings;
package codons1;
sub new {
    my $class = shift;
    return bless {}, $class;
}
sub bar {
    my %some_hash = (temperature=>"35");
    return %some_hash;
}
1;
package main;
my $object = codons1->new(); #creates the object of codons1
my %temp = $object->bar(); #call the bar method from codons1 object
print keys %temp;

- Perl. perlootut, perlobj. - Perl Perl.

+2

Source: https://habr.com/ru/post/1544635/


All Articles