如何读取 Perl 中传递给构造函数的参数和 `use Module` 传递的参数?
目前我正在制作一个新模块,我想知道如何在我的模块中实现 2 件事。
我们经常看到这样的use
:
use My::Module qw(something);
例如:
use CGI::Carp qw(fatalsToBrowser);
所以第一个问题是,我该如何 检索这个,我的意思是是否 用户指定了任何内容以及内容 他指定了?
第二个问题是,我如何传递并读取参数 直接在构造函数上,例如 这个:
my $my_module = My::Module->new(arg1,arg2,arg3);
AS 在评论中请求简单模块测试代码:
package My::Module;
# $Id$
use strict;
use Carp;
sub new {
my $class = shift;
my $self = {};
$self->{ARG1} = undef;
$self->{ARG2} = undef;
$self->{ARG3} = undef;
$self->{ARG4} = undef;
bless($self,$class);
return $self;
}
sub arg1 {
my $self = shift;
if (@_) { $self->{ARG1} = shift }
return $self->{ARG1};
}
sub arg2 {
my $self = shift;
if (@_) { $self->{ARG2} = shift }
return $self->{ARG2};
}
sub arg3 {
my $self = shift;
if (@_) { $self->{ARG3} = shift }
return $self->{ARG3};
}
sub arg4 {
my $self = shift;
if (@_) { $self->{ARG4} = shift }
return $self->{ARG4};
}
sub dump {
my $self = shift;
require Data::Dumper;
my $d = Data::Dumper->new([$self], [ref $self]);
$d->Deepcopy(1);
return $d->Dump();
}
1; # so the require or use succeeds
Currently I am making a new module and I was wondering how could I implement in my module 2 things.
We often see the use
like:
use My::Module qw(something);
for example:
use CGI::Carp qw(fatalsToBrowser);
So the first question is, how do i
retrieve this, i mean wether the
user has specified anything and what
he specified ?Second question is, How do i pass and read the args
directly on the constructor like
this:my $my_module = My::Module->new(arg1,arg2,arg3);
AS requested on the comment the simple module test code:
package My::Module;
# $Id$
use strict;
use Carp;
sub new {
my $class = shift;
my $self = {};
$self->{ARG1} = undef;
$self->{ARG2} = undef;
$self->{ARG3} = undef;
$self->{ARG4} = undef;
bless($self,$class);
return $self;
}
sub arg1 {
my $self = shift;
if (@_) { $self->{ARG1} = shift }
return $self->{ARG1};
}
sub arg2 {
my $self = shift;
if (@_) { $self->{ARG2} = shift }
return $self->{ARG2};
}
sub arg3 {
my $self = shift;
if (@_) { $self->{ARG3} = shift }
return $self->{ARG3};
}
sub arg4 {
my $self = shift;
if (@_) { $self->{ARG4} = shift }
return $self->{ARG4};
}
sub dump {
my $self = shift;
require Data::Dumper;
my $d = Data::Dumper->new([$self], [ref $self]);
$d->Deepcopy(1);
return $d->Dump();
}
1; # so the require or use succeeds
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
perldoc -f use
解释了use
关键字 只是在编译时加载模块,然后对其调用->import
。调用者提供给use
语句的参数将被传递给 import 方法调用。至于你的第二个问题:构造函数只是方法。获取它们的参数就像获取任何其他方法或函数一样,使用
@_< /代码>变量
。
perldoc -f use
explains that theuse
keyword is simply loading a module during compile-time, and then calling->import
on it. The arguments a caller gave to theuse
statement will be passed to the import method call.As for your second question: constructors are just methods. Getting their arguments works like it does for any other method or function, using the
@_
variable.import
子例程获取在use
中传递的参数。以下代码示例应该对您有帮助。文件:My/Module.pm
文件:module.pl
如果您正在编写面向对象的模块,您可以尝试Moose 这将为您节省大量时间。
import
subroutine gets the arguments passed in ause
. The following code samples should help you.File: My/Module.pm
File: module.pl
If you are programming an object oriented module, you may try Moose which will save you lots of time.