Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MatchContext {
- has $.name;
- has $.available is rw;
- }
- class Option {
- has $.name;
- has $.supply is rw;
- method callback() {
- $!supply.tap(
- -> $v {
- given $v {
- if .available {
- if .name eq $!name {
- .available = False;
- }
- }
- }
- }
- );
- }
- }
- sub getopt(@args is copy = @*ARGS) {
- my $p = Supplier.new;
- my @options = [
- Option.new(name => "a", supply => $p.Supply),
- Option.new(name => "b", supply => $p.Supply),
- ];
- @options[0].callback();
- @options[1].callback();
- sub parse(Supply $source --> Supply) {
- supply {
- whenever $source {
- if .starts-with('-') || .starts-with('--') {
- note "In Parser: Emit the option {.Str}";
- $p.emit(
- my $mc = MatchContext.new(
- name => .substr(1),
- :available
- ));
- emit "The {.Str} matched ? " ~ ($mc.available ?? 'FAILED' !! 'OK');
- }
- }
- }
- }
- my $parse = parse(Supply.from-list(@args));
- react {
- whenever $parse -> $msg {
- say "In MAIN: GOT MESSAGE: ", $msg;
- }
- }
- note "done the supplier";
- $p.done;
- }
- &getopt();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement