我正在学习 Perl 对象。我在模块文件create_schedules.pm中编写了一个简单的构造函数:
#!/usr/bin/perl -w
#
package create_schedules;
use strict;
use warnings;
use diagnostics;
sub new {
my $class = shift;
my %params = @_;
my $self=bless{
_para1=>$params{'mypara1'},
_para2=>$params{'mypara2'}
},$class;
return $self;
}
1;
我正在主文件main.pl中创建一个对象:
#!/usr/bin/perl -w
use strict;
use warnings;
use diagnostics;
use lib::my_module;
sub _start(){
print "Main Function Started\n";
create_schedules::new(
'mypara1' => 'This is mypara1',
'mypara2' => 'This is mypara2',
);
}
_start();
当我运行 main.pl 时,我得到了以下错误:
Main Function Started
Odd number of elements in hash assignment at lib/create_schedules.pm line 9 (#1)
(W misc) You specified an odd number of elements to initialize a hash,
which is odd, because hashes come in key/value pairs.