I wrote a grep-like program in perl6, and now I made it into parallel processing. But I ran into some problem: even with the same command line the program sometimes succeeds, and sometimes fails. When it succeeds, things looks just normal to me. When it fails, I don't know why...
Here is the error message when it fails.
> 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
And the code is:
#!/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;
}
}
}
My question is why it fails sometimes?
Regards
Xin