如何读取 Perl 中传递给构造函数的参数和 `use Module` 传递的参数?

发布于 2024-09-24 18:06:15 字数 1514 浏览 2 评论 0原文

目前我正在制作一个新模块,我想知道如何在我的模块中实现 2 件事。

我们经常看到这样的use

use My::Module qw(something);

例如:

use CGI::Carp qw(fatalsToBrowser);
  1. 所以第一个问题是,我该如何 检索这个,我的意思是是否 用户指定了任何内容以及内容 他指定了?

  2. 第二个问题是,我如何传递并读取参数 直接在构造函数上,例如 这个:

    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);
  1. So the first question is, how do i
    retrieve this, i mean wether the
    user has specified anything and what
    he specified ?

  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

宁愿没拥抱 2024-10-01 18:06:15

perldoc -f use 解释了 use 关键字 只是在编译时加载模块,然后对其调用 ->import 。调用者提供给 use 语句的参数将被传递给 import 方法调用。

至于你的第二个问题:构造函数只是方法。获取它们的参数就像获取任何其他方法或函数一样,使用 @_< /代码>变量

perldoc -f use explains that the use keyword is simply loading a module during compile-time, and then calling ->import on it. The arguments a caller gave to the use 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.

无力看清 2024-10-01 18:06:15

import 子例程获取在 use 中传递的参数。以下代码示例应该对您有帮助。

文件:My/Module.pm

package My::Module;

use warnings;
use strict;

use Data::Dumper;

sub import {
    my ( $package, @args ) = @_;

    print Dumper \@args;
}

1;

文件:module.pl

#!/usr/bin/env perl

use warnings;
use strict;

use My::Module qw(something);

如果您正在编写面向对象的模块,您可以尝试Moose 这将为您节省大量时间。

import subroutine gets the arguments passed in a use. The following code samples should help you.

File: My/Module.pm

package My::Module;

use warnings;
use strict;

use Data::Dumper;

sub import {
    my ( $package, @args ) = @_;

    print Dumper \@args;
}

1;

File: module.pl

#!/usr/bin/env perl

use warnings;
use strict;

use My::Module qw(something);

If you are programming an object oriented module, you may try Moose which will save you lots of time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文