Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I call a Perl class with a shorter name?

Tags:

module

perl

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

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

like image 241
Galaxy Avatar asked Sep 16 '09 02:09

Galaxy


1 Answers

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

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

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();

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.

like image 57
cjm Avatar answered Nov 01 '22 16:11

cjm