相关疑难解决方法(0)

Perl模块方法调用:不能在$ {SOMEFILE}行$ {SOMELINE}的未定义值上调用方法"X"

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

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

模块: ./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;
}
Run Code Online (Sandbox Code Playgroud)

脚本: ./test.pl

#!/usr/bin/perl

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

print $My::Module->trim( " \t hello world\t \t" );
Run Code Online (Sandbox Code Playgroud)

我收到了错误消息

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

事实上,如果我调用$My::Module->notamethod( "hello world" );它会产生类似的错误.

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

这个错误Can't call method “X” on an …

perl module function dbi

6
推荐指数
2
解决办法
1万
查看次数

标签 统计

dbi ×1

function ×1

module ×1

perl ×1