代码之家  ›  专栏  ›  技术社区  ›  p_agarwal

如何解析传递给定义为在Perl中接受2个参数的子例程的一个参数

  •  1
  • p_agarwal  · 技术社区  · 7 年前

    我正在创建一个新对象,如下所示:

    my $new_obj = new P_module({key => 'abc'});
    

    P_模块的构造函数定义如下:

    sub new {
        my ($pkg, $input) = @_;
        my $obj = {};
        bless ($obj, ref($pkg)||$pkg);
        $obj->{key} = $input->{key};
     }
    

    pkg 应该是 key input 应该是 abc obj 来拿钥匙 钥匙 $input->{key} 甚至意味着?

    4 回复  |  直到 7 年前
        1
  •  6
  •   ikegami    7 年前

    首先

    my $new_obj = new P_module({ key => 'abc' });
    

    最好写为

    my $new_obj = P_module->new({ key => 'abc' });
    

    这是的缩写

    my %anon = ( key => 'abc' );
    my $new_obj = P_module->new(\%anon);
    

    在进行方法调用时,invocant(函数的剩余部分 -> )作为第一个参数传递。这意味着 $pkg P_module $input 引用是否由返回 { key => 'abc' } .

    自从 是对散列的引用, $input->{key} 获取具有键的元素的值 key


    sub new {
        my ($class, %args) = @_;
        my $self = bless({}, $class);      # If base class.
        #my $self = $class->SUPER::new();  # If inheriting.
        $self->{key} = $args{$key};
        return $self;
     }
    
     my $obj = P_module->new( key => 'abc' );
    

    $existing_obj->new . 它还使用了更多的标准名称 $class $self

        2
  •  2
  •   Andy Lester    7 年前

    你有这样的理由吗:

    bless ($obj, ref($pkg)||$pkg);
    

    而不是这个?

    bless ($obj, $pkg);
    

    如果没有,那么我假设你削减&从其他代码粘贴到某处。你可能想要的是后者。

    ref($pkg)||$pkg 允许您执行以下操作:

    my $new_object = $existing_obj->new;
    

    而不是

    my $new_object = new Classname;
    

    既然你可能不需要这样做,那就坚持下去吧

    bless($obj$pkg);
    
        3
  •  0
  •   Greg Bacon    7 年前

    周围的花括号创建散列 参考 或者hashref,它是单个标量。参数值将为

    • $pkg -包含包的名称,可能是 "P_module"
    • $input

    代码 $input->{key} "key" ,由调用方提供

    my $new_obj = new P_module({key => 'abc'});
    

    这样写被认为是更好的风格

    my $new_obj = P_module->new({key => 'abc'});
    

    my $new_obj = P_module->new({key => 'abc', other => 'unused'});
    

    “密钥” 使用复制到对象的状态

    $obj->{key} = $input->{key};
    

    P_module::new 成为

    bless ($obj, ref($pkg)||$pkg);
    

    因为这将返回可用于调用其他方法的受祝福对象, 例如 ,

    my $new_obj = new P_module({key => 'abc'});
    print $new_obj->other_method_that_you_must_define(), "\n";
    
        4
  •  0
  •   zdim    6 年前

    bless

    你通过一个 hash引用 {...} ,什么是标量。因此分配给 $input ,假设“公平”传递的类名被分配给 $pkg .

    sub new {
        my ($class, $input) = @_;
        my $self = { %$input };
        return bless $self, $class;
    }
    

    return return bless { %$input }, $class; ,初始化(匿名)hashref,并将其放入其包中 $class (所以它是一个对象),并返回。

    使解析所有这些成为可能。

    但是,通常需要处理参数。首先,你 必须

    sub new {
        my ($class, $input) = @_;
        my $self = {};
        bless $self, $class;       # $self is an object now
        $self->_init($input);      # on which methods can be called
        return $self;
    }
    
    sub _init {
        my ($self, $args) = @_;
        # Check arguments, work out defaults (etc), 
        # then assign to self as appropriate
        %$self = %$args;
    }
    

    其中代码通过引用写入“back”,以便 $self new 已更新。

    这应该与正常的方法调用一起使用

    my $new_obj = P_module->new({key => 'abc', other => '...'});
    

    new P_module . 构造函数,主要称为 bless 它是一个引用(通常是散列引用),因此传递的是类名而不是对象。因此,它“知道”它的包,并且是一个对象。

    perlmod (一个类就是一个包),教程 perlootut ,和参考 perlobj

    Moose Moo .