perl6需要帮助来了解有关proto正则表达式/令牌/规则的更多信息

时间:2021-06-11 00:05:04

The following code is taken from perl6 website tutorial, and I am trying to learn more about it before more experimentation:

以下代码来自perl6网站教程,我试图在更多实验之前了解更多信息:

proto token command {*}
      token command:sym<create>   { <sym> }
      token command:sym<retrieve> { <sym> }
      token command:sym<update>   { <sym> }
      token command:sym<delete>   { <sym> }
  1. Is the * in the first line a whatever-star? Can it be something else, such as

    第一行中的*是一个什么明星?它可以是别的东西,比如

    proto token command { /give me an apple/ }
    
  2. Can "sym" be something else, such as

    “sym”可以是其他东西,例如

    command:eat<apple> { <eat> } ?
    

1 个解决方案

#1


7  

{*} tells the runtime to call the correct candidate.
Rather than force you to write {{*}} for the common case of just call the correct one, the compiler allows you to shorten it to just {*}

{*}告诉运行时调用正确的候选者。编译器允许您将其缩短为{*},而不是强迫您为只调用正确的一般情况编写{{*}}。

That is the case for all proto routines like sub, method, regex, token, and rule.

对于所有原型例程,例如sub,method,regex,token和rule,就是这种情况。

In the case of the regex proto routines, only a bare {*} is allowed.
The main reason is probably because no-one has really come up with a good way to make it work sensibly in the regex sub-language.

在正则表达式原型例程的情况下,只允许裸{*}。主要原因可能是因为没有人真正想出一种让它在正则表达式子语言中合理运行的好方法。

So here is an example of a proto sub that does some things that are common to all of the candidates.

所以这里是一个proto sub的例子,它做了一些对所有候选人都很常见的事情。

#! /usr/bin/env perl6
use v6.c;
for @*ARGS { $_ = '--stdin' when '-' }

# find out the number of bytes
proto sub MAIN (|) {
  try {
    # {*} calls the correct multi
    # then we get the number of elems from its result
    # and try to say it
    say {*}.elems #            <-------------
  }
  # if {*} returns a Failure note the error message to $*ERR
  or note $!.message;
}

#| the number of bytes on the clipboard
multi sub MAIN () {
  xclip
}

#| the number of bytes in a file
multi sub MAIN ( Str $filename ){
  $filename.IO.slurp(:!chomp,:bin)
}

#| the number of bytes from stdin
multi sub MAIN ( Bool :stdin($)! ){
  $*IN.slurp-rest(:bin)
}

sub xclip () {
  run( «xclip -o», :out )
  .out.slurp-rest( :bin, :close );
}

#1


7  

{*} tells the runtime to call the correct candidate.
Rather than force you to write {{*}} for the common case of just call the correct one, the compiler allows you to shorten it to just {*}

{*}告诉运行时调用正确的候选者。编译器允许您将其缩短为{*},而不是强迫您为只调用正确的一般情况编写{{*}}。

That is the case for all proto routines like sub, method, regex, token, and rule.

对于所有原型例程,例如sub,method,regex,token和rule,就是这种情况。

In the case of the regex proto routines, only a bare {*} is allowed.
The main reason is probably because no-one has really come up with a good way to make it work sensibly in the regex sub-language.

在正则表达式原型例程的情况下,只允许裸{*}。主要原因可能是因为没有人真正想出一种让它在正则表达式子语言中合理运行的好方法。

So here is an example of a proto sub that does some things that are common to all of the candidates.

所以这里是一个proto sub的例子,它做了一些对所有候选人都很常见的事情。

#! /usr/bin/env perl6
use v6.c;
for @*ARGS { $_ = '--stdin' when '-' }

# find out the number of bytes
proto sub MAIN (|) {
  try {
    # {*} calls the correct multi
    # then we get the number of elems from its result
    # and try to say it
    say {*}.elems #            <-------------
  }
  # if {*} returns a Failure note the error message to $*ERR
  or note $!.message;
}

#| the number of bytes on the clipboard
multi sub MAIN () {
  xclip
}

#| the number of bytes in a file
multi sub MAIN ( Str $filename ){
  $filename.IO.slurp(:!chomp,:bin)
}

#| the number of bytes from stdin
multi sub MAIN ( Bool :stdin($)! ){
  $*IN.slurp-rest(:bin)
}

sub xclip () {
  run( «xclip -o», :out )
  .out.slurp-rest( :bin, :close );
}