2017-02-17 5 views
5

プロト正規表現/トークン/ルールについての詳細を理解するのに役立つ必要があり、私はより多くの実験の前にそれについての詳細を学ぶためにしようとしています:Perl6のは、次のコードは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. 最初の行の*は何の星ですか?それは、このような

    proto token command { /give me an apple/ } 
    
  2. として、何か他のことができ、 "SYM" は、

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

答えて

7

{*}として、何か他のことができ、正しい候補を呼び出すためにランタイムに指示します。
だけではなく、正しいものを呼び出すの一般的なケースのために{{*}}を書くことを強制、コンパイラはあなたがちょうど{*}

それを短縮することができますsubmethodregextokenようにすべてのprotoルーチンの場合です、およびrule

プロトルーチンの場合、裸の{*}のみが許されます。
主な理由はおそらく誰も本当に正規表現のサブ言語で賢明に動作するようにするための良い方法を考え出したからです。

ここには、すべての候補者に共通するいくつかのことを行う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); 
} 
+0

ありがとう、Brad Gilbert !! – lisprogtor

関連する問題