Perl对象错误:无法通过包定位对象方法 [英] Perl objects error: Can't locate object method via package

查看:268
本文介绍了Perl对象错误:无法通过包定位对象方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我意识到在以太坊中有一些类似的问题,但是我无法解决我的问题.也许我应该改善横向思维.

I realise there are several questions like this out in the ether, but I can't a solution for my problem. Maybe I should improve my lateral thinking.

我有一个正在测试的模块.该模块如下所示:

I have a module which I am testing. This module looks something like:

package MyModule;
use strict;
use warnings;

... # a bunch of 'use/use lib' etc.

sub new {
    my $class = shift;
    my ($name,$options) = @_;

    my $self = {
        _name     => $name,
        _features => $options,
        _ids      => undef,
        _groups   => undef,
        _status   => undef,
    };
    bless $self,$class;
    return $self;
}

sub init {
    my ($self) = @_;
    my ($ids,$groups,$status) = ...; # these are from a working module
    $self->{_ids}    = $ids;
    $self->{_groups} = $groups;
    $self->{_status} = $status;
    return $self;
}

这是我的测试文件:

#!/usr/bin/perl -w

use strict;
use MyModule;
use Test::More tests => 1;
use Data::Dumper;

print "Name: ";
my $name;
chomp($name = <STDIN>);
print "chosen name: $name\n";

my %options = (
    option1 => 'blah blah blah',
    option2 => 'blu blu blu',
);

my $name_object = MyModule->new($name,\%options);
print Dumper($name_object);
isa_ok($name_object,'MyModule'); 

$name_object->init;
print Dumper($name_object);

现在它可以工作到isa_ok,但随后出现了:

Now it works down to the isa_ok, but then comes up with:

Can't locate object method "init" via package "MyModule" at test_MyModule.t line 31, <STDIN> line 1.

这只是在我尝试(似乎有些失败)现在才使用对象时才发生.因此,我认为我误解了Perl中对象的应用!任何帮助将不胜感激...

This has only occurred now that I'm trying (and somewhat failing it seems) to use objects. So thus I reckon I'm misunderstanding the applications of objects in Perl! Any help would be appreciated...

推荐答案

我认为您正在加载的文件与您认为正在加载的文件不同.

I think you're loading a different file than the one you think you are loading.

print($INC{"MyModule.pm"}, "\n");

会告诉您实际加载的文件. (如果模块名称确实是Foo::Bar格式,请使用$INC{"Foo/Bar.pm"}.)确保package的大写字母与文件名匹配.

will tell you which file you actually loaded. (If the module name is really of the form Foo::Bar, use $INC{"Foo/Bar.pm"}.) Make sure the capitalisation of the package and the file name match.

这篇关于Perl对象错误:无法通过包定位对象方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆