Perl模块方法调用:无法调用方法“X”在$ {SOMEFILE}行$ {SOMELINE}处有一个未定义的值 [英] Perl Module Method Calls: Can't call method "X" on an undefined value at ${SOMEFILE} line ${SOMELINE}

查看:213
本文介绍了Perl模块方法调用:无法调用方法“X”在$ {SOMEFILE}行$ {SOMELINE}处有一个未定义的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到处都是,特别是在DBI,我看到这个消息一直都在出现。这很令人困惑,因为首先想到的是我传递函数的参数设置为undef(或类似的),但显然不是这样。



给定一个模块和相应的脚本...



模块: ./ lib / My / Module.pm

  package My :: Module; 

使用strict;
使用警告;

副修剪{
my $ str = shift;
$ str =〜s {\A \s +} {} xms; #从字符串前删除空格
$ str =〜s {\ s + \z} {} xms; #从字符串结尾删除空格
return $ str;
}

脚本: ./ test.pl
$ $ p $ #!/ usr / bin / perl

use strict;
使用警告;
使用My :: Module qw(trim);

print $ My :: Module-> trim(\t hello world\t \t);

我找回错误讯息


无法在./text.pl第7行的未定义值上调用方法trim。

事实上,如果我打电话给 $ My :: Module-> notamethod(hello world); ,它会给出类似的错误。



上面的脚本/模块有什么问题?

这个错误是什么无法调用方法X $ {SOMEFILE}行$ {SOMELINE} 中的未定义值真的在说吗?
这是指方法调用的上下文(传递给这里打印)还是参数的上下文?

解决方案

该语法在变量 $ My :: Module 中查找对象或类名并调用其trim方法,但该变量未定义。



相反,您只想说 print My :: Module :: trim(\t hello world \t \t); 来调用My :: Module :: trim()函数。

在使用行中,看起来您正试图将trim()本地包,所以你可以在没有 My :: Module :: 限定的情况下调用它,但是你的模块看起来并不像它被设置为支持导出。



在你的正则表达式中,/ s和/ m标志没有任何作用 - 它们只改变。,^和$匹配,并且你不使用任何那些。


All over the place, especially in DBI, I see this message come up all the time. It's confusing, because the first thing that comes to mind is that the arguments I'm passing the function are set to undef (or something similar), but it's clearly not the case.

Given a module and a corresponding script...

Module: ./lib/My/Module.pm

package My::Module;

use strict;
use warnings;

sub trim {
    my $str = shift;
    $str =~ s{ \A \s+ }{}xms; # remove space from front of string
    $str =~ s{ \s+ \z }{}xms; # remove space from end of string
    return $str;
}

Script: ./test.pl

#!/usr/bin/perl

use strict;
use warnings;
use My::Module qw(trim);

print $My::Module->trim( " \t hello world\t \t" );

I get back the error message

Can't call method "trim" on an undefined value at ./text.pl line 7.

Infact, if I call $My::Module->notamethod( "hello world" ); it gives a similar error.

What's wrong with the above script/module?

What is that error Can't call method "X" on an undefined value at ${SOMEFILE} line ${SOMELINE} really saying? Does this refer to the context of the method call (passed here to print), or the context of the arguments?

解决方案

That syntax is looking for an object or classname in the variable $My::Module and calling its trim method, but that variable is undefined.

Instead, you want to just say print My::Module::trim( " \t hello world\t \t" ); to call the My::Module::trim() function.

From the use line, it looks like you are trying to import trim() into the local package so you can just call it without the My::Module:: qualification, but your module doesn't look like it is set up to support exporting.

In your regexes, the /s and /m flags don't have any effect - they only change what ., ^, and $ match, and you don't use any of those.

这篇关于Perl模块方法调用:无法调用方法“X”在$ {SOMEFILE}行$ {SOMELINE}处有一个未定义的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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