Perl错误:can';t调用方法";encpass“;关于未定义的值-为什么?

Perl错误:can';t调用方法";encpass“;关于未定义的值-为什么?,perl,perl-module,Perl,Perl Module,以下代码作为“root”用户有什么问题: chpwd.pl #!/usr/bin/perl use Unix::PasswdFile; use strict; use warnings; $pw = new Unix::PasswdFile "/etc/passwd"; $pw->passwd("johndoe", $pw->encpass("newpass")); $pw->commit(); undef $pw; 每当我尝试执行上述脚本时,都会出现以下错误: 无法在/

以下代码作为“root”用户有什么问题:

chpwd.pl

#!/usr/bin/perl
use Unix::PasswdFile;

use strict;
use warnings;

$pw = new Unix::PasswdFile "/etc/passwd";
$pw->passwd("johndoe", $pw->encpass("newpass"));
$pw->commit();
undef $pw;
每当我尝试执行上述脚本时,都会出现以下错误:

无法在/var/www/chpwd.pl第5行对未定义的值调用方法“encpass”

我确认文件
/etc/passwd
不是空的或符号链接,它拥有

rw-r--r--root:root 1022 passwd

系统中确实存在用户
johndoe

请帮忙。谢谢

跟踪输出(部分):

Unix::ConfigFile::mode(/usr/local/share/perl/5.10.1/Unix/ConfigFile.pm:152):
152:我的$this=班次;
Unix::ConfigFile::mode(/usr/local/share/perl/5.10.1/Unix/ConfigFile.pm:153):
153:返回$this->{mode},除非@;
Unix::ConfigFile::new(/usr/local/share/perl/5.10.1/Unix/ConfigFile.pm:64):
64:$this->lock()或返回undef;
Unix::ConfigFile::lock(/usr/local/share/perl/5.10.1/Unix/ConfigFile.pm:165):
165:我的$this=班次;
Unix::ConfigFile::lock(/usr/local/share/perl/5.10.1/Unix/ConfigFile.pm:167):
167:如果($this->locking eq“none”)返回1;
Unix::ConfigFile::锁定(/usr/local/share/perl/5.10.1/Unix/ConfigFile.pm:127):
127:我的$this=班次;
Unix::ConfigFile::锁定(/usr/local/share/perl/5.10.1/Unix/ConfigFile.pm:129):
129:返回$this->{locking},除非@;
Unix::ConfigFile::lock(/usr/local/share/perl/5.10.1/Unix/ConfigFile.pm:168):
168:如果$this->{locked},则返回0;
Unix::ConfigFile::lock(/usr/local/share/perl/5.10.1/Unix/ConfigFile.pm:169):
169:if($this->locking eq“flock”){
Unix::ConfigFile::锁定(/usr/local/share/perl/5.10.1/Unix/ConfigFile.pm:127):
127:我的$this=班次;
Unix::ConfigFile::锁定(/usr/local/share/perl/5.10.1/Unix/ConfigFile.pm:129):
129:返回$this->{locking},除非@;
Unix::ConfigFile::锁定(/usr/local/share/perl/5.10.1/Unix/ConfigFile.pm:127):
127:我的$this=班次;
Unix::ConfigFile::锁定(/usr/local/share/perl/5.10.1/Unix/ConfigFile.pm:129):
129:返回$this->{locking},除非@;
Unix::ConfigFile::lock(/usr/local/share/perl/5.10.1/Unix/ConfigFile.pm:174):
174:my$fh=新IO::File$this->lockfile,O|u CREAT | O|u EXCL | O|u RDWR;
Unix::ConfigFile::lockfile(/usr/local/share/perl/5.10.1/Unix/ConfigFile.pm:145):
145:我的$this=班次;
Unix::ConfigFile::lockfile(/usr/local/share/perl/5.10.1/Unix/ConfigFile.pm:146):
146:@?$this->{lockfile}=shift:$this->{lockfile};
IO::File::new(/usr/lib/perl/5.10/IO/File.pm:35):
35:我的$type=班次;
IO::File::new(/usr/lib/perl/5.10/IO/File.pm:36):
36:my$class=ref($type)| |$type | |“IO::File”;
IO::File::new(/usr/lib/perl/5.10/IO/File.pm:37):
37:@>=0&&@u>=SUPER::new();
IO::Handle::new(/usr/lib/perl/5.10/IO/Handle.pm:54):
54:my$class=ref($[0])|$[0]| |“IO::Handle”;
IO::Handle::new(/usr/lib/perl/5.10/IO/Handle.pm:55):
55:@==1或发出“用法:新$class”;
IO::Handle::new(/usr/lib/perl/5.10/IO/Handle.pm:56):
56:my$io=gensym;
Symbol::gensym(/usr/share/perl/5.10/Symbol.pm:23):
23:my$name=“GEN”。$genseq++;
Symbol::gensym(/usr/share/perl/5.10/Symbol.pm:24):
24:my$ref=\*{$genpkg.$name};
Symbol::gensym(/usr/share/perl/5.10/Symbol.pm:24):
24:my$ref=\*{$genpkg.$name};
Symbol::gensym(/usr/share/perl/5.10/Symbol.pm:25):
25:删除$$genpkg{$name};
Symbol::gensym(/usr/share/perl/5.10/Symbol.pm:26):
26:$ref;
IO::Handle::new(/usr/lib/perl/5.10/IO/Handle.pm:57):
57:祝福$io,$class;
IO::File::new(/usr/lib/perl/5.10/IO/File.pm:40):
40:如果(@){
IO::File::new(/usr/lib/perl/5.10/IO/File.pm:41):
41:$fh->打开(@)
42:或返回未定义;
IO::File::open(/usr/lib/perl/5.10/IO/File.pm:52):
52:     @_ >= 2 && @_  2) {
IO::File::open(/usr/lib/perl/5.10/IO/File.pm:55):
55:my($mode,$perms)=@uz[2,3];
IO::File::open(/usr/lib/perl/5.10/IO/File.pm:56):
56:if($mode=~/^\d+$/){
IO::File::open(/usr/lib/perl/5.10/IO/File.pm:57):
57:定义的$perms或$perms=0666;
IO::File::open(/usr/lib/perl/5.10/IO/File.pm:58):
58:返回sysopen($fh、$file、$mode、$perms);
Unix::ConfigFile::lock(/usr/local/share/perl/5.10.1/Unix/ConfigFile.pm:175):
175:除非定义,否则返回0($fh);
Unix::ConfigFile::new(/usr/local/share/perl/5.10.1/Unix/ConfigFile.pm:104):
104:我的$this=班次;
Unix::ConfigFile::new(/usr/local/share/perl/5.10.1/Unix/ConfigFile.pm:106):
106:$this->unlock()或croak“无法解锁文件:$!”;
Unix::ConfigFile::unlock(/usr/local/share/perl/5.10.1/Unix/ConfigFile.pm:184):
184:我的$this=班次;
Unix::ConfigFile::unlock(/usr/local/share/perl/5.10.1/Unix/ConfigFile.pm:193):
193:如果($this->locking eq“none”)返回1;
Unix::ConfigFile::锁定(/usr/local/share/perl/5.10.1/Unix/ConfigFile.pm:127):
127:我的$this=班次;
Unix::ConfigFile::锁定(/usr/local/share/perl/5.10.1/Unix/ConfigFile.pm:129):
129:返回$this->{locking},除非@;
Unix::ConfigFile::unlock(/usr/local/share/perl/5.10.1/Unix/ConfigFile.pm:194):
194:返回0,除非$this->{locked};
Carp::croak(/usr/share/perl/5.10/Carp.pm:44):
44:sub-croak{die shortmess@}
Carp::shortmess(/usr/share/perl/5.10/Carp.pm:29):
29:sub shortmess{goto&shortmess_jmp}
Carp::shortmess_real(/usr/share/perl/5.10/Carp/Heavy.pm:51):
51:local@CARP_NOT=caller();
Carp::shortmess_real(/usr/share/perl/5.10/Carp/Heavy.pm:52):
52:短重(@);
Carp::shortmess_-heavy(/usr/share/perl/5.10/Carp/heavy.pm:241):
241:如果$Verbose,则返回longmesss_-heavy(@_);
更新:


上面的脚本在我的本地计算机上运行得非常好,而不是在服务器计算机上。

显然,构造函数返回的是
undef
,而不是新创建的对象。下面是构造函数的一部分源代码(在
ConfigFile.pm
中):

你可以自己追踪它,然后在
Unix::ConfigFile::mode(/usr/local/share/perl/5.10.1/Unix/ConfigFile.pm:152):
152:        my $this = shift;
Unix::ConfigFile::mode(/usr/local/share/perl/5.10.1/Unix/ConfigFile.pm:153):
153:        return $this->{mode} unless @_;
Unix::ConfigFile::new(/usr/local/share/perl/5.10.1/Unix/ConfigFile.pm:64):
64:     $this->lock() or return undef;
Unix::ConfigFile::lock(/usr/local/share/perl/5.10.1/Unix/ConfigFile.pm:165):
165:        my $this = shift;
Unix::ConfigFile::lock(/usr/local/share/perl/5.10.1/Unix/ConfigFile.pm:167):
167:        return 1 if ($this->locking eq "none");
Unix::ConfigFile::locking(/usr/local/share/perl/5.10.1/Unix/ConfigFile.pm:127):
127:        my $this = shift;
Unix::ConfigFile::locking(/usr/local/share/perl/5.10.1/Unix/ConfigFile.pm:129):
129:        return $this->{locking} unless @_;
Unix::ConfigFile::lock(/usr/local/share/perl/5.10.1/Unix/ConfigFile.pm:168):
168:        return 0 if $this->{locked};
Unix::ConfigFile::lock(/usr/local/share/perl/5.10.1/Unix/ConfigFile.pm:169):
169:        if ($this->locking eq "flock") {
Unix::ConfigFile::locking(/usr/local/share/perl/5.10.1/Unix/ConfigFile.pm:127):
127:        my $this = shift;
Unix::ConfigFile::locking(/usr/local/share/perl/5.10.1/Unix/ConfigFile.pm:129):
129:        return $this->{locking} unless @_;
Unix::ConfigFile::locking(/usr/local/share/perl/5.10.1/Unix/ConfigFile.pm:127):
127:        my $this = shift;
Unix::ConfigFile::locking(/usr/local/share/perl/5.10.1/Unix/ConfigFile.pm:129):
129:        return $this->{locking} unless @_;
Unix::ConfigFile::lock(/usr/local/share/perl/5.10.1/Unix/ConfigFile.pm:174):
174:        my $fh = new IO::File $this->lockfile, O_CREAT|O_EXCL|O_RDWR;
Unix::ConfigFile::lockfile(/usr/local/share/perl/5.10.1/Unix/ConfigFile.pm:145):
145:        my $this = shift;
Unix::ConfigFile::lockfile(/usr/local/share/perl/5.10.1/Unix/ConfigFile.pm:146):
146:        @_ ? $this->{lockfile} = shift : $this->{lockfile};
IO::File::new(/usr/lib/perl/5.10/IO/File.pm:35):
35:     my $type = shift;
IO::File::new(/usr/lib/perl/5.10/IO/File.pm:36):
36:     my $class = ref($type) || $type || "IO::File";
IO::File::new(/usr/lib/perl/5.10/IO/File.pm:37):
37:     @_ >= 0 && @_ <= 3
38:     or croak "usage: new $class [FILENAME [,MODE [,PERMS]]]";
IO::File::new(/usr/lib/perl/5.10/IO/File.pm:39):
39:     my $fh = $class->SUPER::new();
IO::Handle::new(/usr/lib/perl/5.10/IO/Handle.pm:54):
54:     my $class = ref($_[0]) || $_[0] || "IO::Handle";
IO::Handle::new(/usr/lib/perl/5.10/IO/Handle.pm:55):
55:     @_ == 1 or croak "usage: new $class";
IO::Handle::new(/usr/lib/perl/5.10/IO/Handle.pm:56):
56:     my $io = gensym;
Symbol::gensym(/usr/share/perl/5.10/Symbol.pm:23):
23:     my $name = "GEN" . $genseq++;
Symbol::gensym(/usr/share/perl/5.10/Symbol.pm:24):
24:     my $ref = \*{$genpkg . $name};
Symbol::gensym(/usr/share/perl/5.10/Symbol.pm:24):
24:     my $ref = \*{$genpkg . $name};
Symbol::gensym(/usr/share/perl/5.10/Symbol.pm:25):
25:     delete $$genpkg{$name};
Symbol::gensym(/usr/share/perl/5.10/Symbol.pm:26):
26:     $ref;
IO::Handle::new(/usr/lib/perl/5.10/IO/Handle.pm:57):
57:     bless $io, $class;
IO::File::new(/usr/lib/perl/5.10/IO/File.pm:40):
40:     if (@_) {
IO::File::new(/usr/lib/perl/5.10/IO/File.pm:41):
41:     $fh->open(@_)
42:         or return undef;
IO::File::open(/usr/lib/perl/5.10/IO/File.pm:52):
52:     @_ >= 2 && @_ <= 4 or croak 'usage: $fh->open(FILENAME [,MODE [,PERMS]])';
IO::File::open(/usr/lib/perl/5.10/IO/File.pm:53):
53:     my ($fh, $file) = @_;
IO::File::open(/usr/lib/perl/5.10/IO/File.pm:54):
54:     if (@_ > 2) {
IO::File::open(/usr/lib/perl/5.10/IO/File.pm:55):
55:     my ($mode, $perms) = @_[2, 3];
IO::File::open(/usr/lib/perl/5.10/IO/File.pm:56):
56:     if ($mode =~ /^\d+$/) {
IO::File::open(/usr/lib/perl/5.10/IO/File.pm:57):
57:         defined $perms or $perms = 0666;
IO::File::open(/usr/lib/perl/5.10/IO/File.pm:58):
58:         return sysopen($fh, $file, $mode, $perms);
Unix::ConfigFile::lock(/usr/local/share/perl/5.10.1/Unix/ConfigFile.pm:175):
175:        return 0 unless defined($fh);
Unix::ConfigFile::new(/usr/local/share/perl/5.10.1/Unix/ConfigFile.pm:104):
104:        my $this = shift;
Unix::ConfigFile::new(/usr/local/share/perl/5.10.1/Unix/ConfigFile.pm:106):
106:        $this->unlock() or croak "Can't unlock file: $!";
Unix::ConfigFile::unlock(/usr/local/share/perl/5.10.1/Unix/ConfigFile.pm:184):
184:        my $this = shift;
Unix::ConfigFile::unlock(/usr/local/share/perl/5.10.1/Unix/ConfigFile.pm:193):
193:        return 1 if ($this->locking eq "none");
Unix::ConfigFile::locking(/usr/local/share/perl/5.10.1/Unix/ConfigFile.pm:127):
127:        my $this = shift;
Unix::ConfigFile::locking(/usr/local/share/perl/5.10.1/Unix/ConfigFile.pm:129):
129:        return $this->{locking} unless @_;
Unix::ConfigFile::unlock(/usr/local/share/perl/5.10.1/Unix/ConfigFile.pm:194):
194:        return 0 unless $this->{locked};
Carp::croak(/usr/share/perl/5.10/Carp.pm:44):
44: sub croak   { die  shortmess @_ }
Carp::shortmess(/usr/share/perl/5.10/Carp.pm:29):
29: sub shortmess { goto &shortmess_jmp }
Carp::shortmess_real(/usr/share/perl/5.10/Carp/Heavy.pm:51):
51:     local @CARP_NOT = caller();
Carp::shortmess_real(/usr/share/perl/5.10/Carp/Heavy.pm:52):
52:     shortmess_heavy(@_);
Carp::shortmess_heavy(/usr/share/perl/5.10/Carp/Heavy.pm:241):
241:      return longmess_heavy(@_) if $Verbose;
# Get a filehandle
my $fh = new IO::File $this->filename, $this->mode;
return undef unless defined($fh);
$this->fh($fh);

# Do file locking - this must happen before read is called or we could
# end up with stale data in memory
if ($this->mode eq "r") {
  $this->lock("shared") or return undef;
}
else {
  $this->lock() or return undef;
}

# Initialize object structure from the file
if (exists $opt{readopts}) {
  $this->read($this->fh, $opt{readopts}) or return undef;
}
else {
  $this->read($this->fh) or return undef;
}
return $this;
use strict;
use warnings;
die "Failed to read PW" unless (defined $pw);