僞類SUPER
引用它出現的包的父類(不是調用它的對象的父類!)。你沒有父類。添加一個父類來查看它的工作。這是一個有一些修改的例子。
#######################
package ParentSample;
use strict;
use warnings;
sub new {
my($class, %fields) = @_;
# some debugging statements so you can see where you are:
print "I'm in ", __PACKAGE__, " for $class\n";
# make the object in only one class
bless \%fields, $class;
}
#######################
package Sample;
use strict;
use warnings;
use base qw(ParentSample);
sub new {
my($class) = shift;
# some debugging statements so you can see where you are:
print "I'm in ", __PACKAGE__, " for $class\n";
my %fields = (
Debug => 0,
Error => undef,
);
# let the parent make the object
$class->SUPER::new(%fields);
}
#######################
package main;
use strict;
use warnings;
my $obj = Sample->new(cat => 'Buster');
print "Howdy, sample\n";
奇怪的是,這個錯誤信息在最近版本的perl中變得更好了。您老Perl不會把SUPER
的消息:
$ perl5.8.9 sample
Can't locate object method "new" via package "sample" at sample line 14.
$ perl5.10.1 sample
Can't locate object method "new" via package "sample" at sample line 14.
$ perl5.12.1 sample
Can't locate object method "new" via package "sample::SUPER" at sample line 14.
$ perl5.14.1 sample
Can't locate object method "new" via package "sample::SUPER" at sample line 14.
你在哪裏有'ref($ proto)|| $ proto'來自? – daxim