我在perl6中写了一个类似grep的程序,现在我把它编成了并行处理.但是我遇到了一些问题:即使使用相同的命令行,程序有时会成功,有时会失败.当它成功时,事情对我来说是正常的.当它失败时,我不知道为什么……
这是失败时的错误消息.
- > grep6 perl *
- An operation first awaited:
- in sub MAIN at /Users/xxx/DropBox/bin/grep6 line 28
- in block <unit> at /Users/xxx/DropBox/bin/grep6 line 30
- Died with the exception:
- Cannot find method 'Any' on object of type Match
- in regex at /Users/xxx/DropBox/bin/grep6 line 34
- in sub do_something at /Users/xxx/DropBox/bin/grep6 line 34
- in block at /Users/xxx/DropBox/bin/grep6 line 24
代码是:
- #!/usr/bin/env perl6
- constant $color_red = "\e[31m";
- constant $color_off = "\e[0m";
- sub MAIN(Str $pattern,*@filenames){
- my $channel = Channel.new();
- $channel.send($_) for @filenames; # dir();
- $channel.close;
- my @workers;
- for 1..3 -> $n {
- push @workers,start {
- while (my $file = $channel.poll) {
- do_something($pattern,$file);
- }
- }
- }
- await(@workers);
- }
- sub do_something(Str $pattern,Str $filename) {
- #say $filename;
- for $filename.IO.lines -> $line {
- my Str $temp = $line;
- if $temp ~~ s:g/ (<$pattern>) /$color_red$0$color_off/ {
- say $filename ~ ": " ~ $temp;
- }
- }
- }
我的问题是它为什么有时会失败?
问候
鑫
解决方法
这个问题似乎与已知的
rakudo issue for the
race
method基本相同.
我转自:
- if $temp ~~ s:g/ (<$pattern>) /$color_red$0$color_off/ {
至:
- if $temp ~~ s:g/ ($pattern) /$color_red$0$color_off/ {
问题似乎消失了.
正如后面提到的Xin Cheng以及同一文档中所描述的那样,更简单的插值在字面上符合文档示例所阐明.发行票修复了以下问题:
- my $reg = regex { <$pattern> };
- '' ~~ $reg;
导致更新的程序具有类似的解决方法:
- #!/usr/bin/env perl6
- constant $color_red = "\e[31m";
- constant $color_off = "\e[0m";
- sub MAIN(Str $pattern,*@filenames){
- my $channel = Channel.new();
- $channel.send($_) for @filenames; # dir();
- $channel.close;
- my @workers;
- # match seems required for pre-compilation
- '' ~~ (my regex pat_regex { <$pattern> });
- for 1..3 -> $n {
- push @workers,start {
- while (my $file = $channel.poll) {
- do_something(&pat_regex,$file);
- }
- }
- }
- await(@workers);
- }
- sub do_something(Regex $pat_regex,Str $filename) {
- # say $filename;
- for $filename.IO.lines -> $line {
- my Str $temp = $line;
- if $temp ~~ s:g/ ($pat_regex) /$color_red$0$color_off/ {
- say $filename ~ ": " ~ $temp;
- }
- }
- }