当前位置: 首页 > 知识库问答 >
问题:

Perl 6中有哪些重要空间?

赵永逸
2023-03-14

Perl 6清除了其前身中的一些奇怪情况,它不允许在某些位置使用空格,但也允许在其他位置执行作业。空间在哪里重要?如果有一个完整的参考资料就好了,因此我添加了一个社区维基答案,我会用你的代表收入答案更新。感谢您的示例

但是,还要记住Perl 6有unspace,因此可以使用\使空白有效地不可见。例如,不应显示子例程名称及其参数列表,但使用unspace可以:

sub-name    ( @arguments );  # not okay
sub-name\   ( @arguments );  # okay

共有3个答案

苏弘盛
2023-03-14

为什么这个Perl 6提要操作符是“伪语句”?我有一个例子,其中grep的}将作为语句的结尾,因为隐含的分号:

my @rakudo-people = <scott patrick carl moritz jonathan jerry stephen>;
@rakudo-people
    ==> grep { /at/ } \
    ==> map { .uc } ==> my @who-it's-at;
say ~@who-it's-at;

我忘了我在哪里找到这个例子

my @a = [[<foo>],]; # Is that a reduction?

my @a = [[ <foo>],]; # That space certainly means it's not one?
$hash <key>; # Error, indexing uses postfix operators
$hash<key>; # Works

call-this-subroutine    ( @arguments );  # Error
call-this-subroutine( @arguments ); # Works

my $x = 5;
say $x ++;  # Error
say $x++;   # Works

这对于前缀运算符不是问题:

my $x = 5;
say ++ $x;  # Works
say ++$x;   # Works
蒯卓君
2023-03-14

结束模式的位置(此处为结束)决定了此heredoc中的重要空间:

my $letter = q:to/END/;
    Foo, bar, baz
      <- The preceding two spaces are included in the heredoc.
    END

put $letter;

当run为我们提供:

Foo, bar, baz
  <- The preceding two spaces are included in the heredoc.

尽可能不缩进结尾,会导致heredoc中出现更多的空白:

my $letter = q:to/END/;
    Foo, bar, baz
      <- The preceding four spaces are included in the heredoc.
  END

put $letter;
  Foo, bar, baz
    <- The preceding four spaces are included in the heredoc.
my $greeting = " Hello World! ";
my $salutation = "Hello World!";

put $greeting;
put $salutation;
 Hello World!
Hello World!

下面的例子来自https://docs.perl6.org/language/quoting#Literal_strings:_Q。

Q'this will not work!'
Q(this won't work either!)
Q (this is fine, because of space after Q)
Q 'and so is this'

但是其他分隔符可以使用或不使用空格:

Q^This works!^
Q ^so does this^
隆选
2023-03-14
> my %a = (A=>1, B=>2, C=>3);
{A => 1, B => 2, C => 3}
> %a<A>;
1
> %a <A>;
===SORRY!=== Error while compiling:
Missing required term after infix
------> %a <A>⏏;
    expecting any of:
        prefix
        term
> %a\ <A>;
1

同样,插值被空格打破,我不知道在插值环境中使用unspace的方法。但是当使用{}插值代码的结果时,您可以在打开{之后引入空格:

> put "%a<A>";
1
> put "%a <A>";
%a <A>
> put "%a\ <A>";
%a <A>
> put "%a {'A'}"
%a A
> put "%a{<A>}"
1
> put "%a{ <A> }"
1

此循环适用于Perl 5,但不适用于Perl 6:

for (1,2,3){
    print "counting $_!\n";
}
===SORRY!=== Error while compiling testing.p6
Missing block (whitespace needed before curlies taken as a hash subscript?)
at testing.p6:4
------> <BOL>⏏<EOL>
    expecting any of:
        block or pointy block

但最后括号后面的空格纠正了这一点:

for (1,2,3) {
    print "counting $_!\n";
}
counting 1!
counting 2!
counting 3!

这曾经一直困扰着我,直到我了解到控制流构造的条件通常不需要括号(例如forwhileif构造)。

为了避免这种情况,只需省略括号:

for 1,2,3 {
    print "counting $_!\n";
}

但你仍然必须在大括号前留出空间:

for 1,2,3{
    print "counting $_!\n";
}
===SORRY!=== Error while compiling testing.p6
Missing block (whitespace needed before curlies taken as a hash subscript?)
at testing.p6:4
------> <BOL>⏏<EOL>
    expecting any of:
        block or pointy block
 类似资料:
  • 1、RabbitMQ有哪些重要的角色? 客户端、RabbitMQ、服务端。 2、有哪些重要的组件? (1)connectionFactory(连接管理器) 应用程序与RabbitMQ之间建立连接的管理器。 (2)Channel(信道) 消息推送使用的信道。 (3)RoutingKey(路由键) 用于把生产者的数据分配到交换机上。 (4)Exchange(交换机) 用于接受和分配消息。 (5)Bin

  • 本文向大家介绍RabbitMQ 有哪些重要的角色?相关面试题,主要包含被问及RabbitMQ 有哪些重要的角色?时的应答技巧和注意事项,需要的朋友参考一下 RabbitMQ 中重要的角色有:生产者、消费者和代理: 生产者:消息的创建者,负责创建和推送数据到消息服务器; 消费者:消息的接收方,用于处理数据和确认消息; 代理:就是 RabbitMQ 本身,用于扮演“快递”的角色,本身不生产消息,只是扮

  • 本文向大家介绍RabbitMQ 有哪些重要的组件?相关面试题,主要包含被问及RabbitMQ 有哪些重要的组件?时的应答技巧和注意事项,需要的朋友参考一下 ConnectionFactory(连接管理器):应用程序与Rabbit之间建立连接的管理器,程序代码中使用。 Channel(信道):消息推送使用的通道。 Exchange(交换器):用于接受、分配消息。 Queue(队列):用于存储生产者的

  • 本文向大家介绍Kafka的主要API有哪些?相关面试题,主要包含被问及Kafka的主要API有哪些?时的应答技巧和注意事项,需要的朋友参考一下 答:Apache Kafka有4个主要API: 生产者API 消费者API 流 API 连接器API

  • 本文向大家介绍spring 有哪些主要模块?相关面试题,主要包含被问及spring 有哪些主要模块?时的应答技巧和注意事项,需要的朋友参考一下 spring core:框架的最基础部分,提供 ioc 和依赖注入特性。 spring context:构建于 core 封装包基础上的 context 封装包,提供了一种框架式的对象访问方法。 spring dao:Data Access Object

  • 本文向大家介绍行内元素有哪些?块级元素有哪些? 空(void)元素有那些?相关面试题,主要包含被问及行内元素有哪些?块级元素有哪些? 空(void)元素有那些?时的应答技巧和注意事项,需要的朋友参考一下 行内元素:a b span img input select strong 块级元素:div ul ol li dl dt dd h1 h2 h3 h4 p 等 空元素: