什么是 perl 实验特性,postderef?

What is perl experimental feature, postderef?

我看到 use experimental 'postderef'Moxie here on line 8 中被使用。我只是对它的作用感到困惑。 experimental 的手册页也很模糊,

allow the use of postfix dereferencing expressions, including in interpolating strings

任何人都可以展示没有 pragma 时您必须做什么,以及 pragma 使什么变得更容易或可能吗?

这是什么

很简单。它是有起有落的语法糖。不再需要 pragma,因为该功能是 5.24 的核心。但是为了在 5.20 和 5.24 之间支持该功能,必须启用它:use experimental 'postderef'。在提供的示例中,在 Moxie 中,它用于具有 $meta->mro->@*one line;没有它你必须写 @{$meta->mro}.

剧情简介

这些直接来自 D Foy 的博客,以及我编写的用于比较的 Idiomatic Perl。

D Foy example                    Idiomatic Perl
$gimme_a_ref->()->@[0]->%*       %{ $gimme_a_ref->()[0] }
$array_ref->@*                   @{ $array_ref }
get_hashref()->@{ qw(cat dog) }  @{ get_hashref() }{ qw(cat dog) }

这些例子完全由D Foy提供,

D Foy example                    Idiomatic Perl
$array_ref->[0][0]->@*           @{ $array_ref->[0][0] }
$sub->&*                         &some_sub

的参数
  • postderef 允许链接。
  • postderef_qq 使标量字符串的复杂插值变得更容易。

反对意见

D Foy 根本没有提供

  • 失去印记意义。而在您通过查看最左侧的印记知道 "type" 是什么之前。现在,直到您阅读整个链条,您才知道。这似乎破坏了任何关于印记的争论,因为它迫使你在知道预期的内容之前阅读整个链条。也许争论印记是一个好的设计决策的日子已经结束了?但是,话又说回来,perl6 is still all about them。这里缺乏一致性。
  • 重载 -> 表示 类型 。所以现在你有 $type->[0][1]->@* 表示 取消引用为 $type,还有 强制输入 .
  • 切片在基元上没有类似的语法。

    my @foo = qw/foo bar baz quz quuz quuuz/;
    my $bar = \@foo;
    
    # Idiomatic perl array-slices with inclusive-range slicing
    say @$bar[2..4];   # From reference; returns bazquzquuz
    say @foo[2..4];    # From primitive; returns bazquzquuz
    
    # Whizbang thing which has exclusive-range slicing
    say $bar->@[2,4];  # From reference; returns bazquz
                       # Nothing.
    

来源