Advertisement
Guest User

Untitled

a guest
Jul 17th, 2018
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.49 KB | None | 0 0
  1. class MatchContext {
  2.     has $.name;
  3.     has $.available is rw;
  4. }
  5.  
  6. class Option {
  7.     has $.name;
  8.     has $.supply is rw;
  9.  
  10.     method callback() {
  11.         $!supply.tap(
  12.             -> $v {
  13.                 given $v {
  14.                     if .available {
  15.                         if .name eq $!name {
  16.                             .available = False;
  17.                         }
  18.                     }
  19.                 }
  20.             }
  21.         );
  22.     }
  23. }
  24.  
  25. sub getopt(@args is copy = @*ARGS) {
  26.     my $p = Supplier.new;
  27.     my @options = [
  28.         Option.new(name => "a", supply => $p.Supply),
  29.         Option.new(name => "b", supply => $p.Supply),
  30.     ];
  31.  
  32.     @options[0].callback();
  33.     @options[1].callback();
  34.  
  35.     sub parse(Supply $source --> Supply) {
  36.         supply {
  37.             whenever $source {
  38.                 if .starts-with('-') || .starts-with('--') {
  39.                     note "In Parser: Emit the option {.Str}";
  40.                     $p.emit(
  41.                         my $mc = MatchContext.new(
  42.                             name => .substr(1),
  43.                             :available
  44.                     ));
  45.                     emit "The {.Str} matched ? " ~ ($mc.available ?? 'FAILED' !! 'OK');
  46.                 }
  47.             }
  48.         }
  49.     }
  50.  
  51.     my $parse = parse(Supply.from-list(@args));
  52.  
  53.     react {
  54.         whenever $parse -> $msg {
  55.             say "In MAIN: GOT MESSAGE: ", $msg;
  56.         }
  57.     }
  58.  
  59.     note "done the supplier";
  60.     $p.done;
  61. }
  62.  
  63. &getopt();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement