我怎样才能用一个较短的名字叫一个Perl类? [英] How can I call a Perl class with a shorter name?

查看:75
本文介绍了我怎样才能用一个较短的名字叫一个Perl类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用OO编写Perl模块Galaxy::SGE::MakeJobSH.

I am writing a Perl module Galaxy::SGE::MakeJobSH with OO.

我想使用MakeJobSH->new()而不是Galaxy::SGE::MakeJobSH->new(), 或其他一些简称.我该怎么办?

I want to use MakeJobSH->new() instead of Galaxy::SGE::MakeJobSH->new(), or some other shortnames. How can I do that?

推荐答案

您可以建议您的用户使用别名模块以加载您的模块:

You can suggest that your users use the aliased module to load yours:

use aliased 'Galaxy::SGE::MakeJobSH';
my $job = MakeJobSH->new();

或者您可以将类名导出到名为$MakeJobSH;

Or you could export your class name in a variable named $MakeJobSH;

use Galaxy::SGE::MakeJobSH;  # Assume this exports $MakeJobSH = 'Galaxy::SGE::MakeJobSH';
my $job = $MakeJobSH->new();

或者您可以导出一个MakeJobSH函数来返回您的类名称:

Or you could export a MakeJobSH function that returns your class name:

use Galaxy::SGE::MakeJobSH;  # Assume this exports the MakeJobSH function
my $job = MakeJobSH->new();

不过,我不确定这是否是个好主意.人们通常不必经常键入类名.

I'm not sure this is all that great an idea, though. People don't usually have to type the class name all that often.

这是您在课堂上为最后两个选项所做的事情:

Here's what you'd do in your class for the last two options:

package Galaxy::SGE::MakeJobSH;

use Exporter 'import';
our @EXPORT = qw(MakeJobSH $MakeJobSH);

our $MakeJobSH = __PACKAGE__;
sub MakeJobSH () { __PACKAGE__ };

当然,您可能只想选择其中一种方法.我将它们合并在一起以避免重复示例.

Of course, you'd probably want to pick just one of those methods. I've just combined them to avoid duplicating examples.

这篇关于我怎样才能用一个较短的名字叫一个Perl类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆