我是这个网站的新手,所以请耐心等待,如果这个问题已经在其他地方得到了回答。我试图从模块“codons1.pm”调用子程序“bar”,但遇到错误:无法通过包“codons1.pm”找到对象方法“bar”(也许您忘记加载“codons1.pm”)。下午”?)。主脚本如下所示:
use strict;
use warnings;
my $i = 1;
my $pack = "codons$i\.pm";
require $pack;
(my %temp) = $pack->bar();
print keys %INC ;
Run Code Online (Sandbox Code Playgroud)
感谢(Perl objects error: Can't locate object method via package),我能够使用 %INC 验证模块已加载。该模块看起来像:
package codons1;
sub bar{ #some code;
return (%some_hash);}
1;
Run Code Online (Sandbox Code Playgroud)
我正在使用 $i 以便我可以通过循环加载多个类似的模块。欢迎提出任何建议,并提前致谢。
更好的方法来实现您想要实现的目标
#!/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's object
print keys %temp;
Run Code Online (Sandbox Code Playgroud)
您需要学习 Perl 中基本的面向对象编程。从perlootut开始,然后是perlobj。阅读免费的 Beginning Perl 书中的面向对象 Perl 章节。
| 归档时间: |
|
| 查看次数: |
6784 次 |
| 最近记录: |