到处都是,特别是在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 …