了解推送到哈希和|| = []构造. (在boilerplate.t中生成) [英] Understanding pushing to a hash and ||=[] construct. (generated in boilerplate.t)

查看:81
本文介绍了了解推送到哈希和|| = []构造. (在boilerplate.t中生成)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在重新熟悉Perl,并且刚刚使用module-starter初始化了一个新项目.我现在正在尝试了解生成的代码.除了以下指示的行,一切都很好:

I'm re-acquainting myself with Perl, and have just used module-starter to initialise a new project. I'm now trying to understand the generated code. All is fine apart from the follow line indicated :

sub not_in_file_ok {
  my ($filename, %regex) = @_;
  open( my $fh, '<', $filename )
    or die "couldn't open $filename for reading: $!";

  my %violated;

  while (my $line = <$fh>) {
    while (my ($desc, $regex) = each %regex) {
        if ($line =~ $regex) {
            ##I'm having problems here
            push @{$violated{$desc}||=[]}, $.;
        }
    }
  }
  ...
}

我有两个问题:

  1. ||=[].是|后面是|=,还是an或||后跟=[].有人可以跟我说说这里发生的事情吗? (我猜如果哈希为空,则创建一个空的匿名数组以初始化哈希",但我正在努力查看代码是如何形成的.)
  2. push @{$violated{$desc}}, $.我理解这是指将行号分配给哈希%violated的键$desc.但是从我阅读的代码中,查找$violated{$desc}的键desc的值($violated{$desc}部分),然后将该值用作对数组(@{$value}部分)的符号引用,然后将行号压入该数组".我看不到如何协调这两个视图. /li>
  1. The ||=[]. Is this | followed by |=, or is this an or || followed by an =[]. Can someone talk me through what is happening here? (I'm guessing "if the hash is empty the create an empty anonymous array to initialise the hash", but I'm struggling to see how that is formed from the code.)
  2. push @{$violated{$desc}}, $. I understand this to mean "assign the line number to the key $desc for the hash %violated. But from the code I read, "lookup the value of the key desc of $violated{$desc} (the $violated{$desc} part), then use this value as a symbolic reference to an array (the @{$value} part), then push the line number onto that array". I don't see how to reconcile these two views.

我认为在这一行代码中我需要学习很多东西-有人可以帮我遍历它吗?

I think there is a lot for me to learn in this line of code - can someone help me by walking me through it?

推荐答案

  • ||=:这是赋值运算符.例子

    • ||=: this is an assignment operator. Example

      $a ||= $b;
      # corresponds to
      $a = $a || $b;
      

      请参阅man perlop.在您的示例中

      $a ||= [];
      # corresponds to
      $a = $a || [];
      

      即:如果将左操作数定义为空,则分配一个空数组引用

      that is: if the left operand is defined to nothing, otherwise assign an empty array reference

      %violated包含每个值的数组引用.您可以看到这样的内容:

      %violated contains an array reference for each value. You can see it like that:

      my $array_ref = $violated{$desc};
      push @{array_ref}, $.;
      

    • 更详细地写:

        if (! $violated{$desc} ) {
            $violated{$desc} = [];
        }
        my $array_ref = $violated{$desc};
        push @{ $array_ref }, $.;
      

      编辑

      数组和数组引用

      • ()构造的数组,其中包含元素的动态排序列表(在Perl中,数组可以动态增长)

      • an array constructed with () and contains a dynamic ordered list of elements (in Perl arrays can grow dynamically)

      数组引用是对数组的引用(或多或少没有指针算术的指针).您可以使用[]

      an array reference is a reference to an array (more or less a pointer without pointer arithmetic). You can create and array reference with []

      示例

      my @a = ( 1, 2, 3);
      # $a[0] will contain 1
      
      my $array_ref = [ 10, 11, 12 ];
      # array_ref is a _pointer_ to an array containing 10, 11 and 12
      

      要访问数组引用,您需要取消引用:

      To access an array reference you need to dereference it:

      @{ $array_ref };
      
      my @array = @{ $array_ref }; # is valid
      

      您可以将{ $array_ref}作为数组访问

      ${ $array_ref }[0]
      

      现在回到评论中的问题:%violated是具有以下键值对的哈希:字符串($ desc)和数组引用

      Now back to your question in the comment: %violated is an hash with the following key-value pairs: a string ($desc) and an array reference

      这篇关于了解推送到哈希和|| = []构造. (在boilerplate.t中生成)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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