Perl中的嵌套取消引用箭头:省略或不省略? [英] Nested dereferencing arrows in Perl: to omit or not to omit?

查看:203
本文介绍了Perl中的嵌套取消引用箭头:省略或不省略?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Perl中,当您具有嵌套数据结构时,可以省略取消引用箭头为2d和更多嵌套级别。换句话说,以下两个语法是相同的:

In Perl, when you have a nested data structure, it is permissible to omit de-referencing arrows to 2d and more level of nesting. In other words, the following two syntaxes are identical:

my $hash_ref = { 1 => [ 11, 12, 13 ], 3 => [31, 32] };

my $elem1 = $hash_ref->{1}->[1]; 
my $elem2 = $hash_ref->{1}[1]; # exactly the same as above

现在,我的问题是,选择其中一种样式

Now, my question is, is there a good reason to choose one style over the other?

这似乎是一个流行的风格争论的骨头(只是在SO,我不小心碰到 this and 在5分钟的空间)。

It seems to be a popular bone of stylistic contention (Just on SO, I accidentally bumped into this and this in the space of 5 minutes).

到目前为止,几乎没有一个通常的嫌疑人说任何确定的:

So far, almost none of the usual suspects says anything definitive:

  • perldoc merely says "you are free to omit the pointer dereferencing arrow".
  • Conway's "Perl Best Practices" says "whenever possible, dereference with arrows", but it appears to only apply to the context of dereferencing the main reference, not optional arrows on 2d level of nested data structures.
  • "Mastering Perl for Bioinfirmatics" author James Tisdall doesn't give very solid preference either:


锋利的读者可能有
注意到我们似乎忽略
数组之间的箭头运算符
下标。 (毕竟,这些是
匿名数组的匿名数组
的匿名数组等,所以
不应该写为
[$ array - > [$ i] - > [$ j] - > [$ k]?)Perl
允许这样;只需要在变量名和
第一个数组下标之间的箭头运算符
。它
使眼睛更容易,
有助于避免腕管综合征。在
另一方面,你可能更喜欢保持
dereferencing箭头到位,到
清楚你正在处理
引用。您的选择。

"The sharp-witted reader may have noticed that we seem to be omitting arrow operators between array subscripts. (After all, these are anonymous arrays of anonymous arrays of anonymous arrays, etc., so shouldn't they be written [$array->[$i]->[$j]->[$k]?) Perl allows this; only the arrow operator between the variable name and the first array subscript is required. It make things easier on the eyes and helps avoid carpal tunnel syndrome. On the other hand, you may prefer to keep the dereferencing arrows in place, to make it clear you are dealing with references. Your choice."


  • UPDATED Intermediate Perl d foy,建议省略箭头。请参阅下面的brian的完整答案。

  • UPDATED "Intermediate Perl", as per its co-author brian d foy, recommends omitting the arrows. See brian's full answer below.

    UPDATE 更具体地说:re:可读性,在多嵌套表达式的情况下,其中下标本身是表达式,箭头通过更明显地将下标彼此分开而有助于视觉上标记化表达式。

    UPDATE To be more specific re: readability, in case of a multi-nested expression where subscripts themselves are expressions, the arrows help to "visually tokenize" the expressions by more obviously separating subscripts from one another.

    推荐答案

    除非你真的很喜欢打字或过长的行,不要使用箭头,当你不需要它们。下标旁边的下标暗示引用,所以合格的程序员不需要额外

    Unless you really enjoy typing or excessively long lines, don't use the arrows when you don't need them. Subscripts next to subscripts imply references, so the competent programmer doesn't need extra clues to figure that out.

    我不同意它有更多的可读性有额外的箭头。它是绝对不常规的,让他们移动这个词的有趣的部分更远离彼此。

    I disagree that it's more readable to have extra arrows. It's definitely unconventional to have them moving the interesting parts of the term further away from each other.

    中级Perl 中,我们实际上教的参考,我们告诉你省略不必要的箭头。

    In Intermediate Perl, where we actually teach references, we tell you to omit the unnecessary arrows.

    此外,请记住,没有可读性这样的东西。只有你(和其他人)训练你的眼睛识别为模式。你不会逐字阅读的东西,然后弄清楚它们的意思。你看到你以前见过的一些事情,并认可他们。在你所说的基本语法级别,你的可读性只是你识别模式的能力。使用它更容易识别模式,所以你现在做的是更可读,这并不奇怪。新的风格看起来很奇怪,但最终变得更易识别,因此更可读。

    Also, remember there is no such thing as "readability". There is only what you (and others) have trained your eyes to recognize as patterns. You don't read things character-by-character then figure out what they mean. You see groups of things that you've seen before and recognize them. At the base syntax level that you are talking about, your "readability" is just your ability to recognize patterns. It's easier to recognize patterns the more you use it, so it's not surprising that what you do now is more "readable" to you. New styles seem odd at first, but eventually become more recognizable, and thus more "readable".

    您在评论中给出的例子不难读,因为它缺少箭头。仍然很难用箭头阅读:

    The example you give in your comments isn't hard to read because it lacks arrows. It's still hard to read with arrows:

     $expr1->[$sub1{$x}]{$sub2[$y]-33*$x3}{24456+myFunct($abc)}
     $expr1->[$sub1{$x}]->{$sub2[$y]-33*$x3}->{24456+myFunct($abc)}
    



    我写这样的代码,使用这些类型的变量名来提醒下一个编码器关于每个级别的容器类型:

    I write that sort of code like this, using these sorts of variable names to remind the next coder about the sort of container each level is:

    my $index = $sub1{$x};
    my $key1  = $sub2[$y]-33*$x3;
    my $key2  = 24456+myFunct($abc);
    
    $expr1->[ $index ]{ $key1 }{ $key2 };
    

    为了更好,在子程序中隐藏细节所以你从来没有必须直接播放数据结构的混乱。这是更可读的任何他们:

    To make that even better, hide the details in a subroutine (that's what they are there for :) so you never have to play with that mess of a data structure directly. This is more readable that any of them:

      my $value = get_value( $index, $key1, $key2 );
    
      my $value = get_value(
      $sub1{$x},
       $sub2[$y]-33*$x3,
       24456+myFunct($abc)
          );
    

    这篇关于Perl中的嵌套取消引用箭头:省略或不省略?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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