所以,我有一个 perl 模块,我不仅想将它用作模块,还想将它用作独立的实用程序。像这样在模块内部调用 new() 是否正确:
package Module::Module;
sub new(){};
sub some_method(){};
sub _standalone_init(){
$module = __PACKAGE__->new();
#some magic and operations with $module' content
&some_method();
};
__PACKAGE__->_standalone_init() unless caller;
1;
__END__
是的,__PACKAGE__->class_method
完全合法。
但是还有很多其他问题:
$module
应将范围限定在使用它的方法中。__PACKAGE__
.... unless caller;
在模块的顶层)。;
终止子声明/定义。固定的:
package Module::Module;
sub new { ... }
sub some_method { ... }
sub _standalone_init {
my ($class) = @_;
my $object = $class->new();
$object->some_method();
}
__PACKAGE__->_standalone_init() unless caller;
1;
要不就
package Module::Module;
sub new { ... }
sub some_method { ... }
__PACKAGE__->new()->some_method() if !caller;
1;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句